Borislav Hadzhiev
Sun Mar 20 2022·2 min read
Photo by Jeremy Bishop
To solve the "Duplicate identifier error":
skipLibCheck
option to true
in your tsconfig.json
file.npm i -D @types/node@latest
.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.
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.
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.