π Disallow assigning this to a variable.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
this should be used directly. If you want a reference to this from a higher scope, consider using arrow function expression or Function#bind().
// β
const foo = this;
setTimeout(function () {
foo.bar();
}, 1000);
// β
setTimeout(() => {
this.bar();
}, 1000);
// β
setTimeout(function () {
this.bar();
}.bind(this), 1000);// β
const foo = this;
class Bar {
method() {
foo.baz();
}
}
new Bar().method();
// β
class Bar {
constructor(fooInstance) {
this.fooInstance = fooInstance;
}
method() {
this.fooInstance.baz();
}
}
new Bar(this).method();@typescript-eslint/no-this-alias- Equivalent rule from typescript-eslint. If you already use that one, you don't need this rule. It hasallowDestructuring(enabled by default) andallowedNamesoptions.