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
Confusing "duplicate identifier" Typescript error message
提问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'.
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:
这是因为两件事的结合:
tsconfig
not having anyfiles
section. From http://www.typescriptlang.org/docs/handbook/tsconfig-json.htmlIf no "files" property is present in a tsconfig.json, the compiler defaults to including all files in the containing directory and subdirectories. When a "files" property is specified, only those files are included.
Including
typescript
as an npm dependency :node_modules/typescript/
This means that all oftypescript
gets included .... there is an implicitly includedlib.d.ts
in your project anyways (http://basarat.gitbook.io/typescript/content/docs/types/lib.d.ts.html) and its conflicting with the one that ships with the NPM version of typescript.
tsconfig
没有任何files
部分。来自http://www.typescriptlang.org/docs/handbook/tsconfig-json.html如果 tsconfig.json 中不存在“files”属性,则编译器默认包含包含目录和子目录中的所有文件。当指定了“files”属性时,只包含那些文件。
包含
typescript
作为 npm 依赖项:node_modules/typescript/
这意味着所有内容typescript
都包含在内......lib.d.ts
无论如何,您的项目中都隐式包含了一个(http://basarat.gitbook.io/typescript/content/docs/types/lib.d.ts .html) 及其与 NPM 版本的打字稿附带的冲突。
Fix
使固定
Either list files
or include
explicitly https://basarat.gitbook.io/typescript/docs/project/files.html
列出files
或include
明确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:
问题很简单地解决了:
- Deleting the
node_modules
folder - Running
npm install
to get all packages with correct versions
- 删除
node_modules
文件夹 - 运行
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-promise
on my package.json but my tsconfig
was already with target: "es6"
. So I guess there was a conflict with Promise
when compiling.
我有@types/es6-promise
我的 package.json 但我tsconfig
已经有了target: "es6"
. 所以我猜Promise
编译的时候有冲突。
Removing @types/es6-promise
from my package.json
file solved the issue.
@types/es6-promise
从我的package.json
文件中删除解决了这个问题。
回答by kalifa17
Using webpackI came across same error, just in case excluding the .d.ts
file 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 文件夹:-(