Duplicate identifier error in TypeScript [Solved]

avatar
Borislav Hadzhiev

Last updated: Feb 29, 2024
2 min

banner

# Duplicate identifier error in TypeScript [Solved]

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 doesn't contain at least 1 import or export statement, it is considered a global legacy script which is a common cause of the error.

# Add an export {} statement

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

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

add export statement

The code for this article is available on GitHub

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.

# Pulling in typings from multiple versions of a package

Another common cause of the error is that 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 sample above) if you haven't.

# Update your typings

If your error message shows the 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

update your typings

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.

# Delete your node_modules and reinstall your dependencies

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
# 👇️ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock # 👇️ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # 👇️ clean npm cache npm cache clean --force npm install

Restart your IDE and your development server if the error persists.

The code for this article is available on GitHub
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.