prefer-const
Require const for variables that are never reassigned
let tells the reader that the variable changes later. If it never does, const says so, and an assignment added by mistake becomes an error.
A variable that's declared without a value and assigned exactly once, in the same block, is reported too, because the declaration and the assignment can be one const.
--fix changes let to const when every variable in the declaration has an initial value and can be const.
This rule is on by default in TypeScript files.
Configure#
On by default as a warning. To turn it off:
[lint.rules]
"prefer-const" = "off" # or "error"Every option, set to its default:
[lint.rules]
"prefer-const" = ["warn", {
destructuring = "any", # "any" or "all"
ignoreReadBeforeAssign = false,
}]Examples#
let totaltotal is never reassigned. Use const instead. = items.length;
console.log(total);
for (let itemitem is never reassigned. Use const instead. of items) {
console.log(item);
}
let label;
labellabel is never reassigned. Use const instead. = "Total";const total = items.length;
const label = "Total";
let count = 0;
count += 1;
// Assigned in another block, so it cannot be one `const`
let timer;
if (enabled) {
timer = start();
}Options#
| Option | Default | What it does |
|---|---|---|
destructuring | "any" | "any" reports each variable of a destructuring that can be const. "all" reports only if all of them can. |
ignoreReadBeforeAssign | false | Ignore a variable that's read in code written above its only assignment. |
Here id never changes but name does. By default id is reported:
let { idid is never reassigned. Use const instead., name } = user;
name = name.trim();With "all" it isn't, because the whole declaration can't become const:
{ "destructuring": "all" }No problemslet { id, name } = user;
name = name.trim();timer is assigned once, so it's reported by default:
let timertimer is never reassigned. Use const instead.;
function stop() {
clearTimeout(timer);
}
timer = setTimeout(stop, 100);But stop reads timer above the line that assigns it, so the declaration can't move down to the assignment. ignoreReadBeforeAssign allows this:
{ "ignoreReadBeforeAssign": true }No problemslet timer;
function stop() {
clearTimeout(timer);
}
timer = setTimeout(stop, 100);