Urara-Blog/node_modules/.pnpm-store/v3/files/10/95a720b4066707d0d4e784231adcb835165ba8dfe966c29abf50505728a069cf45dc5f9d94fcd7fac1fcdb85bbafc015a8122f5ca61bba74a30c9cb0974594
2022-08-14 01:14:53 +08:00

60 lines
1.2 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
description: 'Disallow aliasing `this`.'
---
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/no-this-alias** for documentation.
This rule prohibits assigning variables to `this`.
## Rule Details
Assigning a variable to `this` instead of properly using arrow lambdas may be a symptom of pre-ES6 practices
or not managing scope well.
Instead of storing a reference to `this` and using it inside a `function () {`:
```js
const self = this;
>
setTimeout(function () {
self.doWork();
});
```
Use `() =>` arrow lambdas, as they preserve `this` scope for you:
```js
setTimeout(() => {
this.doWork();
});
```
Examples of **incorrect** code for this rule:
(see the rationale above)
Examples of **correct** code for this rule:
(see the rationale above)
## Options
You can pass an object option:
```jsonc
{
"@typescript-eslint/no-this-alias": [
"error",
{
"allowDestructuring": false, // Disallow `const { props, state } = this`; true by default
"allowedNames": ["self"] // Allow `const self = this`; `[]` by default
}
]
}
```
## When Not To Use It
If you need to assign `this` to variables, you shouldnt use this rule.