如何在解析模块时阻止打字稿 2.0 遍历所有父目录? [英] How to block typescript 2.0 from walking up all parent directories while resolving modules?

查看:27
本文介绍了如何在解析模块时阻止打字稿 2.0 遍历所有父目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到 Typescript 2.0 (2.1.6) 并开始出现重复标识符"错误.仔细观察后发现 Typescript 开始从所有上层目录(基本上是其他项目)导入 @types.

Upgraded to Typescript 2.0 (2.1.6) and it started giving "Duplicate identifier" errors. After taking a closer look it turned out that Typescript started importing @types from all upper directories (essentially other projects).

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

What should be the configuration to let Typescript to ignore upper node_modules?

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

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

编辑:这是我得到的一个错误示例:

EDIT: Here is an example of error I'm getting:

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

typings/globals/mocha/index.d.ts(30,13): error TS2300: Duplicate identifier 'describe'. ../../../node_modules/@types/jasmine/index.d.ts(9,18): error TS2300: Duplicate identifier 'describe'.

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

listFiles: true shows @types/jasmine being imported from upper folder:

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

如果我重命名上面的 node_modules 文件夹,则构建成功.

If I rename upper node_modules folder then build succeeds.

推荐答案

官方文档 指定当前目录下的node_modules 和所有父级都会被遍历,除非你指定typeRoots.

The official documentation specifies that node_modules in current directory and all parents will be traversed unless you specify typeRoots.

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

So in theory, the answer should be this:

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

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

Since you still want to include the types from the current directory.

很遗憾这对我来说似乎不起作用.

Unfortunately this doesn't seem to work correctly for me.

这篇关于如何在解析模块时阻止打字稿 2.0 遍历所有父目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆