typescript - 编译器无法从 global.d.ts 导入类型

标签 typescript

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/

相关文章:

javascript - typescript - Jasmine 中无效参数的测试用例

typescript 多类型参数

typescript - Typescript 编译器 API - 更改变量名称

typescript Symbol.iterator

实现类中的 typescript 索引签名和方法不起作用

javascript - visual studio typescript 与angular 2冲突

javascript - 在 Visual Studio 2017 中保存或发布时,Typescript 无法编译

javascript - 如何缩小 AMD-Typescript 浏览器项目

html - 如何在我的项目上正确安装 ngx-recaptcha?

typescript - 为什么 typescript 在排除扩展接口(interface)时给出类型 `never`