如何阻止typescript 2.0在解析模块时走遍所有父目录?

时间:2017-02-10 03:19:13

标签: typescript typescript2.0

升级到Typescript 2.0(2.1.6)并开始提供"重复标识符"错误。经过仔细研究后发现,Typescript开始从所有上层目录(实质上是其他项目)导入@types。

让Typescript忽略上层node_modules的配置应该是什么?

src
 └── node_modules << *** how to ignore it? ***
     └── @types

 └── my.app << *** how to build this folder and down only? ***
         └── node_modules
             └── @types

编辑:以下是我遇到的错误示例:

typings / globals / mocha / index.d.ts(30,13):错误TS2300:重复的标识符&#39;描述&#39;。 ../../../node_modules/@types/jasmine/index.d.ts(9,18):错误TS2300:重复的标识符&#39;描述&#39;。

listFiles:true显示从上层文件夹导入@ types / jasmine:

C:/src/<project>/<folder>/<my.app>/typings/globals/mocha/index.d.ts
C:/src/node_modules/@types/jasmine/index.d.ts

如果我重命名上层node_modules文件夹,那么构建成功。

4 个答案:

答案 0 :(得分:1)

official documentation指定将遍历当前目录中的node_modules和所有父项,除非您指定typeRoots

所以从理论上讲,答案应该是:

{
  "compilerOptions": {
    "typeRoots": [
      "./node_modules/@types"
    ]
  }
}

由于您仍希望包含当前目录中的类型。

不幸的是这对我来说似乎不正常。

答案 1 :(得分:0)

您可以在编译器选项中指定根目录。 See the official documentation.

$array2

答案 2 :(得分:0)

我遇到了这个问题,并从文档中推断出与@peterjwest相同的内容-但是在阅读了这个问题:https://github.com/Microsoft/TypeScript/issues/27026之后,我认为我误解了typeRoots的意图(这似乎很有用仅当配置全局类型而不是模块类型时。

在我的情况下,解决方案是为冲突类型(在我的情况下为反应)配置baseUrlpaths

tsconfig.json

{
    "compilerOptions": {
        ...
        "baseUrl": ".",
        "paths": {
            "react": ["node_modules/@types/react"]
        }
    }
    ...
}

这似乎可以告诉打字稿我想从本地node_modules专门解析该类型。

答案 3 :(得分:0)

这可以帮助任何人,在tsconfig.json中设置"types": []对我有用。看到这个github评论。

https://github.com/Microsoft/TypeScript/issues/13992#issuecomment-279020210

相关问题