Block-scoped variable cannot be re-declared
A variable declared with 'let' or 'const' is being re-declared within the same block.
- 1Copy-pasting a variable declaration within the same scope.
- 2Having a function parameter and a variable declaration with the same name inside the function.
- 3Forgetting that a variable has already been declared in the current block.
Redeclaring a 'let' variable in the same scope.
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.
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.
let x = 10; x = 20;
Why this works
Omitting 'let' on the second line reassigns the value instead of redeclaring it.
✕ 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.
microsoft/TypeScript src/compiler/diagnosticMessages.json
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev