Here is the full NPM package on GitHub.
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs"
}
}
全局.d.ts
interface Foo { }
索引.ts
const x: Foo = {};
这是我们构建时发生的事情:
$ \node_modules\.bin\tsc .\index.ts
index.ts(1,10): error TS2304: Cannot find name 'Foo'.
这是我们的版本:
$ .\node_modules\.bin\tsc --version
Version 2.3.4
这些是 tsc 列出的文件:
$ .\node_modules\.bin\tsc --listFiles
C:/temp/node_modules/typescript/lib/lib.d.ts
C:/temp/global.d.ts
C:/temp/index.ts
我们如何自动将 Foo
加载到 index.ts 文件中?
研究
The documentation on global.d.ts表示以上内容应该有效。
最佳答案
您还必须将 global.d.ts
文件作为 tsc
参数的一部分传递:
$ \node_modules\.bin\tsc .\index.ts .\global.d.ts
但请注意,通过指定文件,您将忽略 tsconfig.json
文件。因此,如果您想使用 tsconfig.json
文件,只需调用 tsc
而不带任何参数,它将使用您执行 tsc --listFiles< 时列出的文件
.
来自documentation :
When input files are specified on the command line, tsconfig.json files are ignored.
关于typescript - 编译器无法从 global.d.ts 导入类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44533334/