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();
});
This rule accepts an options object with the following properties:
interface 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: [] },
];
Options
When Not To Use It
If you need to assign this
to variables, you shouldn’t use this rule.