简体   繁体   中英

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

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.

Additionally I want to ship Test.d.ts and Test.js, but NOT Test.ts

Your code doesn't indicate that Test.ts needs to reference Test.d.ts , so just compile Test.ts by itself

tsc Test.ts

instead of

tsc Test.ts Test.d.ts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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