Confusing "duplicate identifier" Typescript error message
TypescriptTypescript Problem Overview
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.
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:
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": false,
"outDir": "built/",
"sourceMap": true,
"target": "es5"
}
}
My 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:
{
"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"
}
}
}
Typescript Solutions
Solution 1 - Typescript
This is because of the combination of two things:
-
tsconfig
not having anyfiles
section. From http://www.typescriptlang.org/docs/handbook/tsconfig-json.html> If 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.
Fix
Either list files
or include
explicitly https://basarat.gitbook.io/typescript/docs/project/files.html
Solution 2 - Typescript
Update: Version 1.0 of Typings changed the output structure and the below answer relates to pre 1.0 version.
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:
{
"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.
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 issue discusses it more.
Solution 3 - Typescript
Problem was solved by simply:
- Deleting the
node_modules
folder - Running
npm install
to get all packages with correct versions
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
Solution 4 - Typescript
If you have installed typings separately under typings folder
{
"exclude": [
"node_modules",
"typings"
]
}
Solution 5 - Typescript
You could also use the exclude option in tsconfig.json file like so:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"exclude": [
"node_modules"
]
}
Solution 6 - Typescript
I just ran into this problem. When I ran npm start
, I got a bunch of duplicate identifier errors.
SOLUTION:
From the project root folder run:
rm -r typings
typings install
npm start
and everything works fine.
Solution 7 - Typescript
Enabling skipLibCheck in tsconfig fixed it for me.
{
"compilerOptions": {
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"exclude": ["node_modules"],
"include": ["./src/**/*.ts"]
}
Solution 8 - Typescript
In my case I got the error as
> node_modules/@types/es6-promise/index.d.ts(11,15): error TS2300: Duplicate identifier 'Promise'.
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.
Removing @types/es6-promise
from my package.json
file solved the issue.
Solution 9 - Typescript
Using webpack I came across same error, just in case excluding the .d.ts
file in your tsconfig.json and node_modules solved my issue:
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts",
"typings/index.d.ts"
]
Solution 10 - Typescript
Adding "typeRoots": ["node_modules/@types"],
to "compilerOptions" in tsconfig.json file worked for me.
Solution 11 - Typescript
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.
Bit of an edge case but leaving an answer here just in case someone else is hunting for this solution.
Solution 12 - Typescript
run the following command will fix this issue.
npm install @types/node --save-dev
Solution 13 - Typescript
Add "skipLibCheck": true to the "compilerOptions"
https://github.com/ionic-team/capacitor/issues/5436#issuecomment-1077942409
Solution 14 - Typescript
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 :-(
Solution 15 - Typescript
I had this error, along with others, after I changed my tsconfig.json to target:"es2015", and module:"es2015".
The base (AngularJS2 quickstart) used /// <reference path="../../typings/index.d.ts" />
in the main.ts file. To solve this, I had to remove that line.
Solution 16 - Typescript
we removed a lib folder from the website folder. this was created by a previous installation of typings. this became duplicate. When this was removed it worked!
Solution 17 - Typescript
It can be because of having both typing and dependency in your node folder. so first check what you have in your @types folder and if you have them in dependencies, remove the duplicate. for me it was core.js
Solution 18 - Typescript
remove this @types/express-validator from package.json file, then run npm install
Author message: This package has been deprecated This is a stub types definition for express-validator (https://github.com/ctavan/express-validator). express-validator provides its own type definitions, so you don't need @types/express-validator installed!
Solution 19 - Typescript
Closing the solution completely and rerunning the project solved my issue.
Solution 20 - Typescript
A (possible) solution for Angular
In my case, when installing @angularfire. It automatically imported environment
from my environments file. Which gave the Duplicate identifier 'environment'
error.
However, I already had this import in the same file, so that caused the duplicate identifier error.
It took me creating a new project and going step by step to find this error...
TL;DR: Check for duplicate imports in the file the error occurs.
Solution 21 - Typescript
I ran into a similar problem. Simply moving my tsconfig.json from the root of my project up to a different scope helped. In my project, I moved tsconfig.json from the root up to wwwroot.