【问题标题】:TypeScript 2.0 throws errors from excluded files?TypeScript 2.0 从排除的文件中抛出错误?
【发布时间】:2023-03-29 17:09:01
【问题描述】:

我有一个基于 this 种子项目的 nodeJS 项目。它有两个 tsconfig.json 文件,如下所示:

{
    "compilerOptions": {
        "target": "es6",
        "module": "system",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false
    },
    "exclude": [
        "../node_modules"
    ]
}

然而,尽管排除了 node_modules,我还是得到了很多错误,其中一些我在下面显示。自从我开始使用新的npm i @types/xyz 方法以来,就出现了这种情况。

[0] node_modules/@types/core-js/index.d.ts(21,14):错误 TS2300:重复标识符“PropertyKey”。

[0] node_modules/@types/core-js/index.d.ts(85,5): 错误 TS2687: 'name' 的所有声明必须具有相同的修饰符。

[0] node_modules/@types/core-js/index.d.ts(145,5):错误 TS2403:后续变量声明必须具有相同的类型。变量 '[Symbol.unscopables]' 必须是类型 '{ copyWithin: boolean;条目:布尔值;填充:布尔值;查找:布尔值; findIndex:布尔值;键:...',但这里有类型 'any'。

[0] node_modules/@types/core-js/index.d.ts(262,5): 错误 TS2687: 'flags' 的所有声明必须具有相同的修饰符。

[0] node_modules/@types/core-js/index.d.ts(276,5):错误 TS2687:“EPSILON”的所有声明必须具有相同的修饰符。

[0] node_modules/@types/core-js/index.d.ts(311,5):错误 TS2687:“MAX_SAFE_INTEGER”的所有声明必须具有相同的修饰符。

【问题讨论】:

    标签: node.js typescript typescript-typings typescript2.0


    【解决方案1】:

    我遇到过各种各样的类似问题;但我从 %localappdata%\Microsoft\Typescript

    中删除了整个 Typescript 文件夹

    然后,我跑了:

    npm install -g typescript@2.1
    

    然后神奇地又开始工作了……

    我确实也重启了几次VS2017,并多次删除了我的解决方案node_modules......这些帮助了我的挫败感,但没有提供解决方案。

    【讨论】:

      【解决方案2】:

      我刚刚在使用 anguler2 项目升级到 ts2.0 后遇到了这个问题。 我现在使用typings 并且有很多依赖项。其中至少有一个包含@types,它复制了一些导入。但它们是必需的,我不能只删除文件夹/依赖项(就像其他人建议的那样)。

      在这里,我找到了一个指向对我有帮助的解决方案的指针: https://github.com/Microsoft/TypeScript/issues/11257

      来自 lucassp 的示例:

      {
          "compilerOptions": {
              "target": "es6",
              "module": "commonjs",
              "moduleResolution": "node",
              "sourceMap": true,
              "emitDecoratorMetadata": true,
              "experimentalDecorators": true,
              "removeComments": false,
              "noImplicitAny": false,
              "types": []
          },
          "exclude": [
              "../node_modules"
          ]
      }
      

      我只需将 "types": [] 添加到 tsconfig.json 文件中,这使得它不包含 @types 中的任何类型(因为它们目前都由类型管理)。

      这里是编译器选项from the doc的描述:

      --types string[]
      List of names of type definitions to include. See @types, –typeRoots and –types for more details.
      

      Microsoft 似乎认为 @types 是新的黑色打字稿定义导入。阅读有关此here 的更多信息。

      最终我可能会迁移,但我会先让尘埃落定。

      【讨论】:

        【解决方案3】:

        我遇到了同样的错误。我没有使用明确的@types/* 声明。但是,看起来更新包已将它们添加为依赖项。

        通过从typings.json中删除条目,这些错误消失了。基本上,node_modules/@types/ 下的任何声明都应该不在typings.json 中重复。

        不要忘记在更新您的 typings.json 文件后运行 typings prune,以便删除多余的输入。

        【讨论】:

          【解决方案4】:

          在我的项目之前,我注意到 重复标识符 问题。我偶然做的是我通过 npm 在我的项目的子目录中安装了一个包。我不得不删除那里生成的node_modules 的子目录来修复它。

          您的错误也与我发现的这篇帖子相似 here

          海报提出以下建议:

          从 package.json 中删除 @types/core-js,删除 node_modules 和 npm 安装解决了这个问题。

          【讨论】:

          • @types/core-js 是其他东西的依赖项......我在我的 CI 上遇到了问题,所以我认为我的 package.json 中没有任何东西跨度>
          猜你喜欢
          • 1970-01-01
          • 2020-08-28
          • 1970-01-01
          • 2018-07-15
          • 1970-01-01
          • 2016-02-21
          • 1970-01-01
          • 2021-11-20
          • 2021-04-09
          相关资源
          最近更新 更多