Declaring the members upfront as class fields makes code more readable and can prevent typos. We would also allow initializing in the constructor, but I think we should auto-fix to class field as it's neater.
Inspired by eslint/eslint#11540.
Any suggestions on the rule name?
class X {
func setName(name) {
this.name = name;
}
func say() {
console.log(this.name);
}
}
class X {
name;
func setName(name) {
this.name = name;
}
func say() {
console.log(this.name);
}
}
How it would prevent typos:
class X {
name;
func setName(name) {
this.name = name;
}
func say() {
// This would have resulted in `undefined` being printed without this rule.
console.log(this.myName); // ESLint error
}
}
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too