【问题标题】:How to block typescript 2.0 from walking up all parent directories while resolving modules?如何在解析模块时阻止 typescript 2.0 遍历所有父目录?
【发布时间】:2017-06-28 07:15:00
【问题描述】:

升级到 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:重复的标识符“描述”。 ../../../node_modules/@types/jasmine/index.d.ts(9,18): error TS2300: Duplicate identifier '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 文件夹,则构建成功。

【问题讨论】:

    标签: typescript typescript2.0


    【解决方案1】:

    在这对任何人都有帮助,在我的 tsconfig.json 中设置 "types": [] 对我有用。请参阅此 github 评论。

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

    【讨论】:

      【解决方案2】:

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

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

      tsconfig.json:

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

      这似乎适用于告诉 typescript 我想从我的本地 node_modules 专门解析该类型。

      【讨论】:

        【解决方案3】:

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

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

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

        因为您仍想包含当前目录中的类型。

        不幸的是这对我来说似乎无法正常工作。

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

          • 使用此设置打字稿抱怨此错误:错误 TS2688:找不到“全局”的类型定义文件。
          • 嗯,您可以尝试将lib 属性添加到您的compilerOptions 或改用exclude
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-04-03
          • 2019-04-14
          • 1970-01-01
          • 2022-06-28
          • 2017-12-12
          • 2016-08-01
          • 1970-01-01
          相关资源
          最近更新 更多