如何在TypeScript中使用声明文件

时间:2022-02-01 23:15:45

There is something I am not getting in TypeScript when it comes to declaration files and 3rd party libraries written in pure Javascript. Let's say I have the following Javascript class:

当涉及声明文件和用纯Javascript编写的第三方库时,我在TypeScript中没有得到什么。假设我有以下Javascript类:

$ cat SomeClass.js

var SomeClass = (function () {
    function SomeClass() {
    }
    SomeClass.prototype.method1 = function () {
            return "some string";
    };
    return SomeClass;
})();
exports.SomeClass = SomeClass;

And I want to get type checking for it, so I create declaration file like this:

我想得到它的类型检查,所以我创建这样的声明文件:

$ cat test.d.ts

class SomeClass {
    public method1(): string;
}

Then I want to use the class and declaration file in some code:

然后我想在一些代码中使用类和声明文件:

$ cat main.ts

///<reference path="./test.d.ts"/>
import ns = module("./SomeClass");

function test(): string {
   var sc = new ns.SomeClass();
   return sc.method1();
}

When I try to compile it, I get this:

当我尝试编译它时,我得到了这个:

$ tsc main.ts
main.ts(2,19): The name '"./SomeClass"' does not exist in the current scope
main.ts(2,19): A module cannot be aliased to a non-module type
main.ts(5,16): Expected var, class, interface, or module

From what I can tell, the import statement requires an actual TypeScript class to exist and the reference statement isn't enough to help the compiler figure out how to handle it.

据我所知,import语句需要存在一个实际的TypeScript类,并且引用语句不足以帮助编译器弄清楚如何处理它。

I tried changing it to

我尝试将其更改为

import ns = module("./test.d");

But no dice either.

但也没有骰子。

The only way I can get this to actually compile and run is to use the require statement instead of import, like this:

实际编译和运行的唯一方法是使用require语句而不是import,如下所示:

$ cat main.ts

///<reference path="./node.d.ts"/>
///<reference path="./test.d.ts"/>
var ns = require("./SomeClass");

function test(): string {
   var sc = new ns.SomeClass();
   return sc.method1();
}

The problem with this code is that TypeScript is not running any type checking. In fact, I can totally remove the line

此代码的问题是TypeScript没有运行任何类型检查。事实上,我可以完全删除该行

///<reference path="./test.d.ts"/>

and it doesn't change anything.

它不会改变任何东西。

However, if I remove the require statement, I can get type checking, but the code blows up at runtime because there is no require statement.

但是,如果我删除require语句,我可以进行类型检查,但代码在运行时会爆炸,因为没有require语句。

$ cat main.ts

///<reference path="./test.d.ts"/>

function test(): string {
   var sc = new SomeClass();
   return sc.method1();
}

test();

$ node main.js

main.js:2
    var sc = new SomeClass();
                 ^
ReferenceError: SomeClass is not defined
    ...

1 个解决方案

#1


7  

cat test.d.ts

declare module "SomeClass.js" {
    class SomeClass {
        method1(): string;
    }
}

cat Main.ts

///<reference path="test.d.ts"/>
import ns = module("SomeClass.js");

function test() {
   var sc = new ns.SomeClass();
   return sc.method1();
}

tsc Main.ts --declarations

tsc Main.ts - 声明

cat Main.js

var ns = require("SomeClass.js")
function test() {
    var sc = new ns.SomeClass();
    return sc.method1();
}

cat Main.d.ts

import ns = module ("SomeClass.js");
function test(): string;

#1


7  

cat test.d.ts

declare module "SomeClass.js" {
    class SomeClass {
        method1(): string;
    }
}

cat Main.ts

///<reference path="test.d.ts"/>
import ns = module("SomeClass.js");

function test() {
   var sc = new ns.SomeClass();
   return sc.method1();
}

tsc Main.ts --declarations

tsc Main.ts - 声明

cat Main.js

var ns = require("SomeClass.js")
function test() {
    var sc = new ns.SomeClass();
    return sc.method1();
}

cat Main.d.ts

import ns = module ("SomeClass.js");
function test(): string;

相关文章