no-this-alias
Disallow aliasing
this
.
✅
Extending "plugin:@typescript-eslint/recommended"
in an ESLint configuration enables this rule.
Assigning a variable to this
instead of properly using arrow lambdas may be a symptom of pre-ES6 practices
or not managing scope well.
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-this-alias": "error"
}
};
Examples
- ❌ Incorrect
- ✅ Correct
const self = this;
setTimeout(function () {
self.doWork();
});
setTimeout(() => {
this.doWork();
});
Options
This rule accepts the following options
type Options = [
{
/** Whether to ignore destructurings, such as `const { props, state } = this`. */
allowDestructuring?: boolean;
/** Names to ignore, such as ["self"] for `const self = this;`. */
allowedNames?: string[];
},
];
const defaultOptions: Options = [
{ allowDestructuring: true, allowedNames: [] },
];
When Not To Use It
If you need to assign this
to variables, you shouldn’t use this rule.