仅在导入键入文件时进行全局扩充

时间:2019-03-28 12:51:29

标签: typescript types visual-studio-code

让我说我有这些文件:

// foo.d.ts
declare global {
    interface Console {
        foo: string
    }
}
// foo.js
console.foo = 'bar';

如何限制global augmentation仅在导入文件foo.js时适用?

// abc.js
require('./foo')
console.foo // should be OK with TS
// Which it is.
// xyz.js
console.foo // should not be OK with TS.
// But TS says that console.foo is defined as a string.

我目前依靠VSCode的默认打字稿集成,并且所有文件在文件的开头都带有//@ts-check标志。

0 个答案:

没有答案