π Prefer class field declarations over this assignments in constructors.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§π‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
Enforces declaring property defaults with class fields instead of setting them inside the constructor.
To avoid leaving empty constructors after autofixing, use the
no-useless-constructorrule.
// β
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';
}// β
class Foo {
#foo = 'foo';
constructor() {
this.#foo = 'bar';
}
}
// β
class Foo {
#foo = 'bar';
}