typescript 令人困惑的“重复标识符”打字稿错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31322525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:42:55  来源:igfitidea点击:

Confusing "duplicate identifier" Typescript error message

typescript

提问by pitosalas

Why am I getting this and many more errors of this kind? I am adding a link to the repo as well as key code snippets below. I think I have a basic misunderstanding of how the dependency and "include" chaining works.

为什么我会收到此错误以及更多此类错误?我正在添加一个指向 repo 的链接以及下面的关键代码片段。我想我对依赖和“包含”链接的工作方式有一个基本的误解。

csvproc(master)> tsc
node_modules/typescript/bin/lib.core.d.ts(83,5): error TS2300: Duplicate identifier 'configurable'.
node_modules/typescript/bin/lib.core.d.ts(84,5): error TS2300: Duplicate identifier 'enumerable'.
node_modules/typescript/bin/lib.core.d.ts(85,5): error TS2300: Duplicate identifier 'value'.
node_modules/typescript/bin/lib.core.d.ts(86,5): error TS2300: Duplicate identifier 'writable'.

All code can be found here.

所有代码都可以在这里找到

My tsconfig.json:

我的 tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": false,
        "outDir": "built/",
        "sourceMap": true,
        "target": "es5"
    }
}

My tsd.json:

我的 tsd.json:

{
  "version": "v4",
  "repo": "borisyankov/DefinitelyTyped",
  "ref": "master",
  "path": "typings",
  "bundle": "typings/tsd.d.ts",
  "installed": {
    "node/node-0.10.d.ts": {
      "commit": "6387999eb899d0ba02d37dd8697647718caca230"
    },
    "should/should.d.ts": {
      "commit": "e1182d56ccb192379eade6055d9ba3fb6a0bacc4"
    }
  }
}

My tsd.d.ts:

我的 tsd.d.ts:

{
  "version": "v4",
  "repo": "borisyankov/DefinitelyTyped",
  "ref": "master",
  "path": "typings",
  "bundle": "typings/tsd.d.ts",
  "installed": {
    "node/node-0.10.d.ts": {
      "commit": "6387999eb899d0ba02d37dd8697647718caca230"
    },
    "should/should.d.ts": {
      "commit": "e1182d56ccb192379eade6055d9ba3fb6a0bacc4"
    }
  }
}

采纳答案by basarat

This is because of the combination of two things:

这是因为两件事的结合:

Fix

使固定

Either list filesor includeexplicitly https://basarat.gitbook.io/typescript/docs/project/files.html

列出filesinclude明确https://basarat.gitbook.io/typescript/docs/project/files.html

回答by RationalDev likes GoFundMonica

Update: Version 1.0 of Typings changed the output structure and the below answer relates to pre 1.0 version.

更新:Types 1.0 版更改了输出结构,以下答案与 1.0 版之前的版本有关。

If you are using Typings and exclude in your tsconfig.json, you may run into the issue of duplicate types and need something like the following:

如果您在 tsconfig.json 中使用 Typings 和 exclude,您可能会遇到重复类型的问题,并且需要类似以下内容:

{
  "exclude": [
    "typings/browser.d.ts",
    "typings/browser",
    "node_modules"
  ]
}

To simplify integration with TypeScript, two files - typings/main.d.ts and typings/browser.d.ts - are generated which reference all the typings installed in the project only one of which can be used at a time.

为了简化与 TypeScript 的集成,生成了两个文件——typings/main.d.ts 和typings/browser.d.ts,它们引用了项目中安装的所有typings,一次只能使用其中一个。

So depending on which version you need, you should exclude (or include) the "browser" or the "main" type files, but not both, as this is where the duplicates come from.

因此,根据您需要的版本,您应该排除(或包含)“浏览器”或“主”类型文件,但不能同时排除,因为这是重复项的来源。

This Typings issuediscusses it more.

这个 Typings 问题对此进行了更多讨论。

回答by Lovjith

If you have installed typings separately under typings folder

如果您在typings 文件夹下单独安装了typings

{
  "exclude": [
    "node_modules",
    "typings"
  ]
}

回答by Alex Klaus

Problem was solved by simply:

问题很简单地解决了:

  1. Deleting the node_modulesfolder
  2. Running npm installto get all packages with correct versions
  1. 删除node_modules文件夹
  2. 运行npm install以获取具有正确版本的所有软件包

In my case, the problem occurred after changing Git branches, where a new branch was using a different set of node modules. The old branch was using TypeScript v1.8, the new one v2.0

就我而言,问题发生在更改 Git 分支之后,其中一个新分支使用了一组不同的节点模块。旧分支使用 TypeScript v1.8,新分支使用 v2.0

回答by user6217332

I just ran into this problem. When I ran npm start, I got a bunch of duplicate identifier errors.

我刚刚遇到了这个问题。当我运行时npm start,我收到了一堆重复的标识符错误。

SOLUTION:

解决方案:

From the project root folder run:

从项目根文件夹运行:

rm -r typings
typings install
npm start

and everything works fine.

一切正常。

回答by thitemple

You could also use the exclude option in tsconfig.json file like so:

您还可以在 tsconfig.json 文件中使用 exclude 选项,如下所示:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": false,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "node_modules"
  ]
}

回答by Felipe Sabino

In my case I got the error as

在我的情况下,我得到了错误

node_modules/@types/es6-promise/index.d.ts(11,15): error TS2300: Duplicate identifier 'Promise'.

node_modules/@types/es6-promise/index.d.ts(11,15): 错误 TS2300: 重复标识符“承诺”。

And I had @types/es6-promiseon my package.json but my tsconfigwas already with target: "es6". So I guess there was a conflict with Promisewhen compiling.

我有@types/es6-promise我的 package.json 但我tsconfig已经有了target: "es6". 所以我猜Promise编译的时候有冲突。

Removing @types/es6-promisefrom my package.jsonfile solved the issue.

@types/es6-promise从我的package.json文件中删除解决了这个问题。

回答by kalifa17

Using webpackI came across same error, just in case excluding the .d.tsfile in your tsconfig.jsonand node_modules solved my issue:

使用webpack我遇到了同样的错误,以防万一排除tsconfig.json和 node_modules 中的.d.ts文件解决了我的问题:

"exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts",
    "typings/index.d.ts"
] 

回答by elwyn

I had this issue caused by having an unexpected folder on disk (jspm_packages, no longer being used) which was not tracked by source control (and hidden from my IDE). This had a duplicate install of TypeScript in it, which caused the issues.

我遇到了这个问题,因为磁盘上有一个意外的文件夹(jspm_packages不再使用),它没有被源代码管理跟踪(并且从我的 IDE 中隐藏)。这其中重复安装了 TypeScript,从而导致了问题。

Bit of an edge case but leaving an answer here just in case someone else is hunting for this solution.

有点边缘情况,但在这里留下一个答案,以防其他人正在寻找这个解决方案。

回答by Bruno

I had this problem and it turns out I had a a second node_modules folder in my project that wasn't supposed to be there :-(

我遇到了这个问题,结果我的项目中有一个不应该存在的第二个 node_modules 文件夹:-(