TS2300
TypeScriptERRORCommonDeclarationHIGH confidence

Duplicate identifier

What this means

An identifier, such as a variable, function, or class name, has been declared more than once in the same scope.

Why it happens
  1. 1Declaring the same variable twice using 'let' or 'const' in the same block.
  2. 2Defining two functions with the same name in the same scope.
  3. 3A class and a function in the same scope share the same name.
How to reproduce

Two variables with the same name are declared in the same scope.

trigger — this will error
trigger — this will error
const name = "Alice";
const name = "Bob";

expected output

error TS2300: Duplicate identifier 'name'.

Fix 1

Rename the identifier

WHEN The identifiers are intended to be distinct.

Rename the identifier
const user = "Alice";
const admin = "Bob";

Why this works

Using unique names for each identifier resolves the conflict.

Fix 2

Reassign the variable

WHEN The intent is to change the value of an existing variable.

Reassign the variable
let name = "Alice";
name = "Bob";

Why this works

Using 'let' allows reassignment without redeclaring the variable.

What not to do

Use 'var' to allow re-declaration

This behavior is often a source of bugs and is disallowed in strict mode.

Sources
Official documentation ↗

microsoft/TypeScript src/compiler/diagnosticMessages.json

MDN: Redeclaring variables

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All TypeScript errors