- Error
- Reported by the type checker
TS2588
Cannot assign to '{0}' because it is a constant.
A const variable is assigned a new value. Declare it with let if it needs to change, or assign to a new variable.
What it means
A const binding can't be reassigned after it's declared. This covers =, compound assignments like +=, and ++/--. The object a const refers to can still be changed through its properties.
Example
const total: number = 1;
total = 2;
export {};
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(2,1): error TS2588: Cannot assign to 'total' because it is a constant.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Declare it with
letif it really changes. - Or keep it
constand put the new value in a new variable.