Confusing "duplicate identifier" Typescript error message

Typescript

Typescript 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'.

All code can be found here.

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 any files 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 of typescript gets included .... there is an implicitly included lib.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:

  1. Deleting the node_modules folder
  2. 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

Read More

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpitosalasView Question on Stackoverflow
Solution 1 - TypescriptbasaratView Answer on Stackoverflow
Solution 2 - TypescriptRationalDev likes GoFundMonicaView Answer on Stackoverflow
Solution 3 - TypescriptAlex KlausView Answer on Stackoverflow
Solution 4 - TypescriptLovjithView Answer on Stackoverflow
Solution 5 - TypescriptthitempleView Answer on Stackoverflow
Solution 6 - Typescriptuser6217332View Answer on Stackoverflow
Solution 7 - TypescriptlesterfernandezView Answer on Stackoverflow
Solution 8 - TypescriptFelipe SabinoView Answer on Stackoverflow
Solution 9 - Typescriptkalifa17View Answer on Stackoverflow
Solution 10 - TypescriptPatryk JanikView Answer on Stackoverflow
Solution 11 - TypescriptelwynView Answer on Stackoverflow
Solution 12 - TypescriptNatarajan GanapathiView Answer on Stackoverflow
Solution 13 - TypescriptMohammadreza AbdoliView Answer on Stackoverflow
Solution 14 - TypescriptBrunoView Answer on Stackoverflow
Solution 15 - TypescriptSako73View Answer on Stackoverflow
Solution 16 - TypescriptBlue CloudsView Answer on Stackoverflow
Solution 17 - TypescriptMahdi ShahbaziView Answer on Stackoverflow
Solution 18 - TypescriptChanakaView Answer on Stackoverflow
Solution 19 - Typescriptkaran chavanView Answer on Stackoverflow
Solution 20 - TypescriptRuben SzekérView Answer on Stackoverflow
Solution 21 - TypescriptTrevor GermainView Answer on Stackoverflow