Skip to main content

no-this-alias

Disallow aliasing this.

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"
}
};
Try this rule in the playground ↗

Examples

const self = this;

setTimeout(function () {
self.doWork();
});
Open in Playground

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.

Resources