Solve - Duplicate identifier error in TypeScript


Borislav Hadzhiev

Sun Mar 20 20222 min read


Photo by Jeremy Bishop

Solve - Duplicate identifier error in TypeScript #

To solve the "Duplicate identifier error":

  1. Make sure you don't have any clashing names of identifiers.
  2. Set the skipLibCheck option to true in your tsconfig.json file.
  3. Update the version of your typings, e.g. npm i -D @types/node@latest.

duplicate identifier error

Sometimes the error is caused when we have use a name that clashes with a globally defined object, a global interface or a member with the same name in a different file (if not using ES modules).

// ⛔️ Duplicate identifier 'Employee'.ts(2300) class Employee {} // ⛔️ Duplicate identifier 'Node'.ts(2300) class Node {} // 👈️ name of global interface // ⛔️ Duplicate identifier 'Array'.ts(2300) class Array {} // 👈️ name of global interface

The Employee class causes the error, because this file is not an ES module. A module is a file that contains at least 1 import or export statement.

If your file does not contain at least 1 import or export statement, it is considered a global legacy script, which is a common cause of the error.

If you don't have anything to export, simply export {}.

// ✅ It's an ES module now class Employee {} export {};

We used the export {} line in our file to mark it as an ES module.

Make sure you haven't used any names of global interfaces or objects like Node, Array, String in your code.

Another common cause of the "Duplicate identifier error" error is - your project is pulling in typings from multiple versions of a package.

Set the skipLibCheck option to true in your tsconfig.json.

{ "compilerOptions": { "skipLibCheck": true, // ... rest }, "include": ["src/**/*"], "exclude": ["node_modules"] }

The skipLibCheck option instructs the compiler to skip type checking for declaration files.

This is needed because sometimes two libraries define two copies of the same type.

Make sure to add node_modules to your exclude array (like in the code snippet above) if you haven't.

If your error message shows a name of a typings package, e.g. /node_modules/typescript/lib....

You have to update the version of the types package. For example, this is how you would update the version of @types/node.

npm i -D @types/node@latest

Make sure to install the latest version of the @types package, because you might have an older version (or multiple versions).

If you have the @types/node package in your dependencies, definitely run npm i -D @types/node@latest as it often causes the error.

If the error is not resolved, try to delete your node_modules and package-lock.json files, re-run npm install and restart your IDE.

rm -rf node_modules package-lock.json npm install

Make sure to restart your IDE and your development server if the error still persists. VSCode glitches often and a reboot solves things sometimes.

Use the search field on my Home Page to filter through my more than 1,000 articles.