简体   繁体   English

TypeScript环境声明和实现在单独的文件中

[英]TypeScript ambient declaration and implementation in separate files

I want to be able to provide an ambient declaration for an object in one file, but provide the implementation in another 我希望能够在一个文件中为对象提供环境声明,但在另一个文件中提供实现

Example: 例:

Test.d.ts 测试文件

interface TestConstructor {
    new(value?: any): void;
    ...
}

interface Test {
    ...
}

declare var Test: TestConstructor;

Test.ts 测试

var Test = (function () {
    function Test(value) {
    }

    return Test;
})();

Error: 错误:

Subsequent variable declarations must have the same type. 后续变量声明必须具有相同的类型。 Variable 'Test' must be of type 'TestConstructor', but here has type '(value: any) => void'. 变量“ Test”必须为“ TestConstructor”类型,但此处的类型为“((值:任何)=>无效”)。

I know this seems to be an odd way of using TypeScript but the reason is that I want to build an object with features that TypeScript doesn't support (closures / value properties) so I can't use classes for this. 我知道这似乎是使用TypeScript的一种奇怪方法,但是原因是我想构建一个具有TypeScript不支持的功能(闭包/值属性)的对象,所以我不能为此使用类。

Additionally I want to ship Test.d.ts and Test.js, but NOT Test.ts 另外,我想运送Test.d.ts和Test.js,但不运送Test.ts

Your code doesn't indicate that Test.ts needs to reference Test.d.ts , so just compile Test.ts by itself 您的代码并不表示Test.ts需要引用Test.d.ts ,因此只需自行编译Test.ts

tsc Test.ts

instead of 代替

tsc Test.ts Test.d.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM