Disallow reassigning const, using, and await using variables
Using the recommended config from @eslint/js in a configuration file enables this rule
Constant bindings cannot be modified. An attempt to modify a constant binding will raise a runtime error.
This rule is aimed to flag modifying variables that are declared using const, using, or await using keywords.
Examples of incorrect code for this rule:
/*eslint no-const-assign: "error"*/
const a = 0;
= 1;
/*eslint no-const-assign: "error"*/
const a = 0;
+= 1;
/*eslint no-const-assign: "error"*/
const a = 0;
++;
/*eslint no-const-assign: "error"*/
if (foo) {
using a = getSomething();
= somethingElse;
}
if (bar) {
await using a = getSomething();
= somethingElse;
}
Examples of correct code for this rule:
/*eslint no-const-assign: "error"*/
const a = 0;
console.log(a);
/*eslint no-const-assign: "error"*/
if (foo) {
using a = getSomething();
a.execute();
}
if (bar) {
await using a = getSomething();
a.execute();
}
/*eslint no-const-assign: "error"*/
for (const a in [1, 2, 3]) { // `a` is re-defined (not modified) on each loop step.
console.log(a);
}
/*eslint no-const-assign: "error"*/
for (const a of [1, 2, 3]) { // `a` is re-defined (not modified) on each loop step.
console.log(a);
}
This rule has no options.
If you don’t want to be notified about modifying variables that are declared using const, using, and await using keywords, you can safely disable this rule.
It is safe to disable this rule when using TypeScript because TypeScript's compiler enforces this check.
This rule was introduced in ESLint v1.0.0-rc-1.
© OpenJS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/latest/rules/no-const-assign