【问题标题】:TypeScript config typeRoots and types not working properlyTypeScript 配置 typeRoots 和类型无法正常工作
【发布时间】:2020-07-12 04:59:51
【问题描述】:

当前设置

这是我的目录结构:

root
├──typings/           # currently empty
├──package.json
├──package-lock.json
├──tsconfig.json
└──main.ts

这是我的tsconfig.json

{
    "compilerOptions": {
        "typeRoots": [
            "./typings"
        ],
        "types": ["Phaser"]  
    }    
}

这是我的main.ts 文件:

let countdownNumber: Phaser.GameObjects.BitmapText;

然后我在终端中运行了以下命令:

tsc --traceResolution

我正在尝试在我的main.ts 文件中使用Phaser 作为全局变量。

预期行为:

应该发生的是 Phaser 不应该得到解决,因为编译器应该开始查看我的自定义文件夹 typings

实际行为:

不知何故,它仍然得到解决。这就是它打印的内容:

类型参考指令“Phaser”已成功解析为“/Applications/XAMPP/xamppfiles/htdocs/phasertest/node_modules/Phaser/types/phaser.d.ts”,包 ID 为“phaser/types/phaser.d.ts” @3.22.0',主要:假。

我不知道它是如何找到它的,因为在我的 typeRoots 中,我没有指定 node_modules

我尝试过的:

我试图排除 node_modules 文件夹,我想也许它们仍然会被编译,这就是发生这种情况的原因,但我无法使 exclude 与以下任何一个一起使用:

  • "exclude": ["node_modules/*"]
  • "exclude": ["./node_modules/*"]
  • "exclude": ["./node_modules"]

但这些都不起作用。

【问题讨论】:

标签: javascript typescript typescript-typings typescript-generics tsconfig


【解决方案1】:

我怀疑这是预期的行为,虽然我同意这不是超级直观

根据Types > What does this affect? 上的tsconfig 文档

此选项不会影响@types/* 如何包含在您的应用程序代码中,例如,如果您有上述 > compilerOptions 示例代码如下:

import * as moment from "moment";
moment().format("MMMM Do YYYY, h:mm:ss a");

moment 导入将是完全类型化的。

当你设置了这个选项时,不要在类型数组中包含一个模块:

  • 不会将全局变量添加到您的项目中(例如节点中的进程,或 Jest 中的期望)
  • 不会将导出显示为自动导入建议

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    相关资源
    最近更新 更多