prefer-class-fields
March 27, 2026 ยท View on GitHub
๐ 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.
Examples
// โ
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';
}