Skip to content

Prefer Class Properties

Suggestion
small-rules/prefer-class-properties

Prefer class properties to assignment of literals in constructors.

Diagnostic Messages

unexpectedAssignment
Constructor assigns a literal value to this.property. Literals are static and known at class definition time. Move to a class property declaration: propertyName = value; at class level. This clarifies intent and reduces constructor complexity.
unexpectedClassProperty
Class property declarations are disabled by rule configuration (mode: 'never'). Move initialization into the constructor: this.propertyName = value; inside constructor().

Configuration

This rule accepts one options object after the severity.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/prefer-class-properties": [
"error",
"always"
]
}
}

Examples

Constructor literal class property
class Foo { constructor() { this.foo = 123; } }
Existing class property declaration
class Foo { foo = "bar"; }