This rule raises an issue when properties are assigned to this in a constructor instead of being declared as class fields.
Modern JavaScript supports class field declarations, which provide a cleaner and more readable way to define class properties compared to constructor assignments.
When properties are assigned in constructors using this.property = value, the code becomes more verbose and harder to read. Class
field declarations offer several advantages:
Class field declarations also work with both public and private properties (using the # prefix), providing a unified approach to
property definition.
This issue affects code maintainability and readability. While it doesn’t cause runtime errors, using outdated patterns makes the codebase harder to understand and maintain. Teams working with modern JavaScript expect to see class field syntax, and constructor-based assignments can appear outdated or unnecessarily verbose.
Move simple property assignments from the constructor to class field declarations. This makes the code more concise and follows modern JavaScript conventions.
class Foo {
constructor() {
this.foo = 'foo'; // Noncompliant
}
}
class Foo {
foo = 'foo';
}