Duplicate identifier error in TypeScript

avatar

Borislav Hadzhiev

Last updated: Mar 20, 2022

banner

Photo from Unsplash

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 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).

index.ts
// ⛔️ 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 {}.

index.ts
// ✅ 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.

typescript:index.ts
{ "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.

shell
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.

shell
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.

Conclusion #

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.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.