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 presets.

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
class Example
Example
{
constructor() {
this.
any
value
= "hello";
}
}
class
class MyError
MyError
extends
var Error: ErrorConstructor
Error
{
constructor(
message: string
message
: string) {
super(
message: string
message
);
this.
Error.name: string
name
= "MyError";
}
}
class
class Counter
Counter
{
constructor() {
this.
any
count
= 0;
this.
any
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 ❤️‍🔥 around the world by the Flint team and contributors.