Skip to content

classFieldDeclarations

Reports assigning literal values to this in constructors instead of using class field declarations.

✅ This rule is included in the ts untyped preset.

When a class constructor assigns a literal value to a property, the assignment can be replaced with a class field declaration. Class field declarations are more concise and clearly express the intent of initializing properties with default values.

This rule only reports assignments of literal values (strings, numbers, booleans, null). Assignments involving variables, function calls, or other dynamic expressions are not flagged.

class Example {
constructor() {
this.value = "hello";
}
}
class MyError extends Error {
constructor(message: string) {
super(message);
this.name = "MyError";
}
}
class Counter {
constructor() {
this.count = 0;
this.enabled = true;
}
}

This rule is not configurable.

If you prefer to keep all property initialization in the constructor for consistency, or if your codebase targets environments that do not support class field declarations, you may disable this rule. Some teams may also prefer the constructor style when properties need to be initialized in a specific order relative to super() calls.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.