A variable declared with let is never assigned again
What it finds
let total = items.length that is never assigned again can be const, which tells the reader that the name keeps its value and lets tools rely on it.
A declaration is reported when every variable it declares has a value from the start and is never assigned again, by =, a compound assignment, ++, -- or a destructuring target, anywhere in the file. A for-in or for-of head counts as giving a value. Files with a with statement or a call of eval can assign variables in ways that cannot be seen and are left alone, and so are files with syntax errors, where an assignment may be hidden in what the parser could not read. Turning let into const keeps what JavaScript code does; in TypeScript a const can get a narrower type, so the fix is offered as unsafe there.
From the rule's source, prefer_const.rs.
function total(items: number[]) { let count = items.length;tx0 reports: count is never assigned again let sum = 0; for (const item of items) { sum += item; } let { a, b } = { a: 1, b: 2 };tx0 reports: a and b are never assigned again let [c, d] = [1, 2]; d = 3; let later; later = 1; for (let key in items) {tx0 reports: key is never assigned again console.log(key); } let i = 0, j = 1; i++; return count + sum + a + b + c + d + later + i + j;} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/ts:prefer-const" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/ts:prefer-const] reason="..."tx0 explain tx0/ts:prefer-const shows the same in the terminal. Found a false positive? Report it.