如何在 TypeScript 中使用声明文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13235257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:46:14  来源:igfitidea点击:

How to use declaration files in TypeScript

javascriptnode.jstypescript

提问by d512

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 编写的声明文件和 3rd 方库时,我在 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 语句而不是导入,如下所示:

$ 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
    ...

回答by Markus Jarderot

cat test.d.ts

猫测试.d.ts

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

cat Main.ts

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 --declarations

cat Main.js

猫 Main.js

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

cat Main.d.ts

cat Main.d.ts

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