💼 This rule is enabled in the ✅ recommended
config.
🔧💡 This rule is automatically fixable by the --fix
CLI option and manually fixable by editor suggestions.
This rule enforces the use of class field declarations for static values, instead of assigning them in constructors using this
.
To avoid leaving empty constructors after autofixing, use the
no-useless-constructor
rule.
// ❌
class Foo {
constructor() {
this.foo = 'foo';
}
}
// ✅
class Foo {
foo = 'foo';
}
// ❌
class MyError extends Error {
constructor(message: string) {
super(message);
this.name = 'MyError';
}
}
// ✅
class MyError extends Error {
name = 'MyError'
}
// ❌
class Foo {
foo = 'foo';
constructor() {
this.foo = 'bar';
}
}
// ✅
class Foo {
foo = 'bar';
}