首页 文章

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

提问于
浏览
4

升级到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

EDIT :这是我得到的错误示例:

typings / globals / mocha / index.d.ts(30,13):错误TS2300:重复标识符'describe' . ../../../node_modules/@types/jasmine/index.d.ts(9,18):错误TS2300:重复标识符'describe' .

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文件夹,那么构建成功 .

2 回答

  • 1

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

    所以在理论上,答案应该是这样的:

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

    因为您仍希望包含当前目录中的类型 .

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

  • 0

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

    {
       "compilerOptions": {
           "typeRoots" : ["./typings"]
       }
    }
    

相关问题