【问题标题】:Compiler fails to import type from global.d.ts编译器无法从 global.d.ts 导入类型
【发布时间】:2017-11-15 22:20:11
【问题描述】:

Here is the full NPM package on GitHub.

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs"
  }
}

global.d.ts

interface Foo { }

index.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 表示上面应该可以工作。

【问题讨论】:

  • 这应该可以。你有tsconfig.json 吗?如果是,请发布。
  • @Saravana 我已经发布了 tsconfig 文件。

标签: typescript


【解决方案1】:

您还必须将global.d.ts 文件作为tsc 参数的一部分传递:

$ \node_modules\.bin\tsc .\index.ts .\global.d.ts

但请注意,通过指定文件,您将忽略 tsconfig.json 文件。因此,如果您想使用 tsconfig.json 文件,只需不带任何参数调用 tsc,它会在您执行 tsc --listFiles 时使用列出的文件。

来自documentation

在命令行中指定输入文件时,tsconfig.json 文件被忽略。

【讨论】:

    猜你喜欢
    • 2016-11-22
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2021-03-18
    • 2014-10-01
    • 2020-02-15
    • 2019-03-10
    • 1970-01-01
    相关资源
    最近更新 更多