TS2448
TypeScriptERRORCommonDeclarationHIGH confidence

Block-scoped variable cannot be re-declared

What this means

A variable declared with 'let' or 'const' is being re-declared within the same block.

Why it happens
  1. 1Copy-pasting a variable declaration within the same scope.
  2. 2Having a function parameter and a variable declaration with the same name inside the function.
  3. 3Forgetting that a variable has already been declared in the current block.
How to reproduce

Redeclaring a 'let' variable in the same scope.

trigger — this will error
trigger — this will error
let x = 10;
let x = 20;

expected output

error TS2451: Cannot redeclare block-scoped variable 'x'.

Fix 1

Rename the second variable

WHEN You need two distinct variables.

Rename the second variable
let x = 10;
let y = 20;

Why this works

Each variable must have a unique identifier within its scope.

Fix 2

Reassign the existing variable

WHEN You intended to update the variable's value.

Reassign the existing variable
let x = 10;
x = 20;

Why this works

Omitting 'let' on the second line reassigns the value instead of redeclaring it.

What not to do

Use 'var' to allow re-declaration

'var' has function-scoping rules that are less intuitive and often lead to bugs, which 'let' and 'const' were designed to solve.

Sources
Official documentation ↗

microsoft/TypeScript src/compiler/diagnosticMessages.json

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

← All TypeScript errors