no-class-assign
Disallow reassigning class members
✅ Recommended
Using the recommended
config from @eslint/js
in a configuration file enables this rule
ClassDeclaration
creates a variable, and we can modify the variable.
class A { }
A = 0;
But the modification is a mistake in most cases.
Rule Details
This rule is aimed to flag modifying variables of class declarations.
Examples of incorrect code for this rule:
Open in Playground
/*eslint no-class-assign: "error"*/
class A { }
= 0;
Open in Playground
/*eslint no-class-assign: "error"*/
= 0;
class A { }
Open in Playground
/*eslint no-class-assign: "error"*/
class A {
b() {
= 0;
}
}
Open in Playground
/*eslint no-class-assign: "error"*/
let A = class A {
b() {
= 0;
// `let A` is shadowed by the class name.
}
}
Examples of correct code for this rule:
Open in Playground
/*eslint no-class-assign: "error"*/
let A = class A { }
A = 0; // A is a variable.
Open in Playground
/*eslint no-class-assign: "error"*/
let A = class {
b() {
A = 0; // A is a variable.
}
}
Open in Playground
/*eslint no-class-assign: 2*/
class A {
b(A) {
A = 0; // A is a parameter.
}
}
When Not To Use It
If you don’t want to be notified about modifying variables of class declarations, you can safely disable this rule.
Handled by TypeScript
It is safe to disable this rule when using TypeScript because TypeScript's compiler enforces this check.
Version
This rule was introduced in ESLint v1.0.0-rc-1.