@typescript-eslint/eslint-plugin 8.33.1-alpha.5 → 8.33.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -10
- package/docs/rules/README.md +0 -57
- package/docs/rules/TEMPLATE.md +0 -36
- package/docs/rules/adjacent-overload-signatures.mdx +0 -105
- package/docs/rules/array-type.mdx +0 -126
- package/docs/rules/await-thenable.mdx +0 -184
- package/docs/rules/ban-ts-comment.mdx +0 -165
- package/docs/rules/ban-tslint-comment.mdx +0 -45
- package/docs/rules/ban-types.md +0 -26
- package/docs/rules/block-spacing.md +0 -15
- package/docs/rules/brace-style.md +0 -15
- package/docs/rules/camelcase.md +0 -15
- package/docs/rules/class-literal-property-style.mdx +0 -112
- package/docs/rules/class-methods-use-this.mdx +0 -135
- package/docs/rules/comma-dangle.md +0 -15
- package/docs/rules/comma-spacing.md +0 -15
- package/docs/rules/consistent-generic-constructors.mdx +0 -87
- package/docs/rules/consistent-indexed-object-style.mdx +0 -105
- package/docs/rules/consistent-return.mdx +0 -51
- package/docs/rules/consistent-type-assertions.mdx +0 -196
- package/docs/rules/consistent-type-definitions.mdx +0 -133
- package/docs/rules/consistent-type-exports.mdx +0 -97
- package/docs/rules/consistent-type-imports.mdx +0 -139
- package/docs/rules/default-param-last.mdx +0 -59
- package/docs/rules/dot-notation.mdx +0 -94
- package/docs/rules/explicit-function-return-type.mdx +0 -359
- package/docs/rules/explicit-member-accessibility.mdx +0 -353
- package/docs/rules/explicit-module-boundary-types.mdx +0 -287
- package/docs/rules/func-call-spacing.md +0 -15
- package/docs/rules/indent.md +0 -15
- package/docs/rules/init-declarations.mdx +0 -12
- package/docs/rules/key-spacing.md +0 -15
- package/docs/rules/keyword-spacing.md +0 -15
- package/docs/rules/lines-around-comment.md +0 -15
- package/docs/rules/lines-between-class-members.md +0 -15
- package/docs/rules/max-params.mdx +0 -54
- package/docs/rules/member-delimiter-style.md +0 -15
- package/docs/rules/member-ordering.mdx +0 -1483
- package/docs/rules/method-signature-style.mdx +0 -124
- package/docs/rules/naming-convention.mdx +0 -755
- package/docs/rules/no-array-constructor.mdx +0 -34
- package/docs/rules/no-array-delete.mdx +0 -44
- package/docs/rules/no-base-to-string.mdx +0 -115
- package/docs/rules/no-confusing-non-null-assertion.mdx +0 -75
- package/docs/rules/no-confusing-void-expression.mdx +0 -148
- package/docs/rules/no-deprecated.mdx +0 -119
- package/docs/rules/no-dupe-class-members.mdx +0 -16
- package/docs/rules/no-duplicate-enum-values.mdx +0 -66
- package/docs/rules/no-duplicate-imports.mdx +0 -17
- package/docs/rules/no-duplicate-type-constituents.mdx +0 -89
- package/docs/rules/no-dynamic-delete.mdx +0 -64
- package/docs/rules/no-empty-function.mdx +0 -94
- package/docs/rules/no-empty-interface.mdx +0 -75
- package/docs/rules/no-empty-object-type.mdx +0 -150
- package/docs/rules/no-explicit-any.mdx +0 -177
- package/docs/rules/no-extra-non-null-assertion.mdx +0 -60
- package/docs/rules/no-extra-parens.md +0 -15
- package/docs/rules/no-extra-semi.md +0 -15
- package/docs/rules/no-extraneous-class.mdx +0 -329
- package/docs/rules/no-floating-promises.mdx +0 -282
- package/docs/rules/no-for-in-array.mdx +0 -67
- package/docs/rules/no-implied-eval.mdx +0 -106
- package/docs/rules/no-import-type-side-effects.mdx +0 -80
- package/docs/rules/no-inferrable-types.mdx +0 -113
- package/docs/rules/no-invalid-this.mdx +0 -16
- package/docs/rules/no-invalid-void-type.mdx +0 -119
- package/docs/rules/no-loop-func.mdx +0 -12
- package/docs/rules/no-loss-of-precision.mdx +0 -17
- package/docs/rules/no-magic-numbers.mdx +0 -131
- package/docs/rules/no-meaningless-void-operator.mdx +0 -61
- package/docs/rules/no-misused-new.mdx +0 -53
- package/docs/rules/no-misused-promises.mdx +0 -314
- package/docs/rules/no-misused-spread.mdx +0 -132
- package/docs/rules/no-mixed-enums.mdx +0 -96
- package/docs/rules/no-namespace.mdx +0 -157
- package/docs/rules/no-non-null-asserted-nullish-coalescing.mdx +0 -60
- package/docs/rules/no-non-null-asserted-optional-chain.mdx +0 -46
- package/docs/rules/no-non-null-assertion.mdx +0 -48
- package/docs/rules/no-parameter-properties.mdx +0 -16
- package/docs/rules/no-redeclare.mdx +0 -79
- package/docs/rules/no-redundant-type-constituents.mdx +0 -102
- package/docs/rules/no-require-imports.mdx +0 -114
- package/docs/rules/no-restricted-imports.mdx +0 -84
- package/docs/rules/no-restricted-types.mdx +0 -70
- package/docs/rules/no-shadow.mdx +0 -143
- package/docs/rules/no-this-alias.mdx +0 -124
- package/docs/rules/no-type-alias.mdx +0 -626
- package/docs/rules/no-unnecessary-boolean-literal-compare.mdx +0 -165
- package/docs/rules/no-unnecessary-condition.mdx +0 -293
- package/docs/rules/no-unnecessary-parameter-property-assignment.mdx +0 -42
- package/docs/rules/no-unnecessary-qualifier.mdx +0 -57
- package/docs/rules/no-unnecessary-template-expression.mdx +0 -108
- package/docs/rules/no-unnecessary-type-arguments.mdx +0 -85
- package/docs/rules/no-unnecessary-type-assertion.mdx +0 -97
- package/docs/rules/no-unnecessary-type-constraint.mdx +0 -61
- package/docs/rules/no-unnecessary-type-conversion.mdx +0 -79
- package/docs/rules/no-unnecessary-type-parameters.mdx +0 -255
- package/docs/rules/no-unsafe-argument.mdx +0 -98
- package/docs/rules/no-unsafe-assignment.mdx +0 -101
- package/docs/rules/no-unsafe-call.mdx +0 -120
- package/docs/rules/no-unsafe-declaration-merging.mdx +0 -65
- package/docs/rules/no-unsafe-enum-comparison.mdx +0 -98
- package/docs/rules/no-unsafe-function-type.mdx +0 -65
- package/docs/rules/no-unsafe-member-access.mdx +0 -81
- package/docs/rules/no-unsafe-return.mdx +0 -126
- package/docs/rules/no-unsafe-type-assertion.mdx +0 -63
- package/docs/rules/no-unsafe-unary-minus.mdx +0 -60
- package/docs/rules/no-unused-expressions.mdx +0 -52
- package/docs/rules/no-unused-vars.mdx +0 -136
- package/docs/rules/no-use-before-define.mdx +0 -98
- package/docs/rules/no-useless-constructor.mdx +0 -21
- package/docs/rules/no-useless-empty-export.mdx +0 -53
- package/docs/rules/no-useless-template-literals.mdx +0 -9
- package/docs/rules/no-var-requires.mdx +0 -77
- package/docs/rules/no-wrapper-object-types.mdx +0 -75
- package/docs/rules/non-nullable-type-assertion-style.mdx +0 -47
- package/docs/rules/object-curly-spacing.md +0 -15
- package/docs/rules/only-throw-error.mdx +0 -150
- package/docs/rules/padding-line-between-statements.md +0 -15
- package/docs/rules/parameter-properties.mdx +0 -522
- package/docs/rules/prefer-as-const.mdx +0 -51
- package/docs/rules/prefer-destructuring.mdx +0 -101
- package/docs/rules/prefer-enum-initializers.mdx +0 -68
- package/docs/rules/prefer-find.mdx +0 -45
- package/docs/rules/prefer-for-of.mdx +0 -50
- package/docs/rules/prefer-function-type.mdx +0 -98
- package/docs/rules/prefer-includes.mdx +0 -81
- package/docs/rules/prefer-literal-enum-member.mdx +0 -111
- package/docs/rules/prefer-namespace-keyword.mdx +0 -51
- package/docs/rules/prefer-nullish-coalescing.mdx +0 -349
- package/docs/rules/prefer-optional-chain.mdx +0 -304
- package/docs/rules/prefer-promise-reject-errors.mdx +0 -78
- package/docs/rules/prefer-readonly-parameter-types.mdx +0 -408
- package/docs/rules/prefer-readonly.mdx +0 -111
- package/docs/rules/prefer-reduce-type-parameter.mdx +0 -66
- package/docs/rules/prefer-regexp-exec.mdx +0 -52
- package/docs/rules/prefer-return-this-type.mdx +0 -93
- package/docs/rules/prefer-string-starts-ends-with.mdx +0 -84
- package/docs/rules/prefer-ts-expect-error.mdx +0 -86
- package/docs/rules/promise-function-async.mdx +0 -143
- package/docs/rules/quotes.md +0 -15
- package/docs/rules/related-getter-setter-pairs.mdx +0 -61
- package/docs/rules/require-array-sort-compare.mdx +0 -89
- package/docs/rules/require-await.mdx +0 -53
- package/docs/rules/restrict-plus-operands.mdx +0 -245
- package/docs/rules/restrict-template-expressions.mdx +0 -167
- package/docs/rules/return-await.mdx +0 -339
- package/docs/rules/semi.md +0 -15
- package/docs/rules/sort-type-constituents.mdx +0 -209
- package/docs/rules/sort-type-union-intersection-members.mdx +0 -16
- package/docs/rules/space-before-blocks.md +0 -15
- package/docs/rules/space-before-function-paren.md +0 -15
- package/docs/rules/space-infix-ops.md +0 -15
- package/docs/rules/strict-boolean-expressions.mdx +0 -184
- package/docs/rules/switch-exhaustiveness-check.mdx +0 -280
- package/docs/rules/triple-slash-reference.mdx +0 -129
- package/docs/rules/type-annotation-spacing.md +0 -15
- package/docs/rules/typedef.mdx +0 -350
- package/docs/rules/unbound-method.mdx +0 -114
- package/docs/rules/unified-signatures.mdx +0 -132
- package/docs/rules/use-unknown-in-catch-callback-variable.mdx +0 -97
@@ -1,84 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow specified modules when loaded by `import`.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-restricted-imports** for documentation.
|
11
|
-
|
12
|
-
It adds support for type import syntaxes:
|
13
|
-
|
14
|
-
- `import type X from "..."`
|
15
|
-
- `import { type X } from "..."`
|
16
|
-
- `import x = require("...")`
|
17
|
-
|
18
|
-
## Options
|
19
|
-
|
20
|
-
This rule adds the following options:
|
21
|
-
|
22
|
-
### `allowTypeImports`
|
23
|
-
|
24
|
-
{/* insert option description */}
|
25
|
-
|
26
|
-
Whether to allow type-only imports for a path.
|
27
|
-
Default: `false`.
|
28
|
-
|
29
|
-
You can specify this option for a specific path or pattern as follows:
|
30
|
-
|
31
|
-
```jsonc
|
32
|
-
{
|
33
|
-
"rules": {
|
34
|
-
"@typescript-eslint/no-restricted-imports": [
|
35
|
-
"error",
|
36
|
-
{
|
37
|
-
"paths": [
|
38
|
-
{
|
39
|
-
"name": "import-foo",
|
40
|
-
"message": "Please use import-bar instead.",
|
41
|
-
"allowTypeImports": true,
|
42
|
-
},
|
43
|
-
{
|
44
|
-
"name": "import-baz",
|
45
|
-
"message": "Please use import-quux instead.",
|
46
|
-
"allowTypeImports": true,
|
47
|
-
},
|
48
|
-
],
|
49
|
-
},
|
50
|
-
],
|
51
|
-
},
|
52
|
-
}
|
53
|
-
```
|
54
|
-
|
55
|
-
Whether to allow [Type-Only Imports](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export).
|
56
|
-
|
57
|
-
Examples of code with the above config:
|
58
|
-
|
59
|
-
<Tabs>
|
60
|
-
<TabItem value="❌ Incorrect">
|
61
|
-
|
62
|
-
```ts option='{"paths":[{"name":"import-foo","message":"Please use import-bar instead.","allowTypeImports":true},{"name":"import-baz","message":"Please use import-quux instead.","allowTypeImports":true}]}'
|
63
|
-
import foo from 'import-foo';
|
64
|
-
export { Foo } from 'import-foo';
|
65
|
-
|
66
|
-
import baz from 'import-baz';
|
67
|
-
export { Baz } from 'import-baz';
|
68
|
-
```
|
69
|
-
|
70
|
-
</TabItem>
|
71
|
-
<TabItem value="✅ Correct">
|
72
|
-
|
73
|
-
```ts option='{"paths":[{"name":"import-foo","message":"Please use import-bar instead.","allowTypeImports":true},{"name":"import-baz","message":"Please use import-quux instead.","allowTypeImports":true}]}'
|
74
|
-
import { foo } from 'other-module';
|
75
|
-
|
76
|
-
import type foo from 'import-foo';
|
77
|
-
export type { Foo } from 'import-foo';
|
78
|
-
|
79
|
-
import type baz from 'import-baz';
|
80
|
-
export type { Baz } from 'import-baz';
|
81
|
-
```
|
82
|
-
|
83
|
-
</TabItem>
|
84
|
-
</Tabs>
|
@@ -1,70 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow certain types.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-restricted-types** for documentation.
|
11
|
-
|
12
|
-
It can sometimes be useful to ban specific types from being used in type annotations.
|
13
|
-
For example, a project might be migrating from using one type to another, and want to ban references to the old type.
|
14
|
-
|
15
|
-
This rule can be configured to ban a list of specific types and can suggest alternatives.
|
16
|
-
Note that it does not ban the corresponding runtime objects from being used.
|
17
|
-
|
18
|
-
## Options
|
19
|
-
|
20
|
-
### `types`
|
21
|
-
|
22
|
-
{/* insert option description */}
|
23
|
-
|
24
|
-
The type can either be a type name literal (`OldType`) or a a type name with generic parameter instantiation(s) (`OldType<MyArgument>`).
|
25
|
-
|
26
|
-
The values can be:
|
27
|
-
|
28
|
-
- A string, which is the error message to be reported; or
|
29
|
-
- An object with the following properties:
|
30
|
-
- `message: string`: the message to display when the type is matched.
|
31
|
-
- `fixWith?: string`: a string to replace the banned type with when the fixer is run. If this is omitted, no fix will be done.
|
32
|
-
- `suggest?: string[]`: a list of suggested replacements for the banned type.
|
33
|
-
|
34
|
-
Example configuration:
|
35
|
-
|
36
|
-
```jsonc
|
37
|
-
{
|
38
|
-
"@typescript-eslint/no-restricted-types": [
|
39
|
-
"error",
|
40
|
-
{
|
41
|
-
"types": {
|
42
|
-
// add a custom message to help explain why not to use it
|
43
|
-
"OldType": "Don't use OldType because it is unsafe",
|
44
|
-
|
45
|
-
// add a custom message, and tell the plugin how to fix it
|
46
|
-
"OldAPI": {
|
47
|
-
"message": "Use NewAPI instead",
|
48
|
-
"fixWith": "NewAPI",
|
49
|
-
},
|
50
|
-
|
51
|
-
// add a custom message, and tell the plugin how to suggest a fix
|
52
|
-
"SoonToBeOldAPI": {
|
53
|
-
"message": "Use NewAPI instead",
|
54
|
-
"suggest": ["NewAPIOne", "NewAPITwo"],
|
55
|
-
},
|
56
|
-
},
|
57
|
-
},
|
58
|
-
],
|
59
|
-
}
|
60
|
-
```
|
61
|
-
|
62
|
-
## When Not To Use It
|
63
|
-
|
64
|
-
If you have no need to ban specific types from being used in type annotations, you don't need this rule.
|
65
|
-
|
66
|
-
## Related To
|
67
|
-
|
68
|
-
- [`no-empty-object-type`](./no-empty-object-type.mdx)
|
69
|
-
- [`no-unsafe-function-type`](./no-unsafe-function-type.mdx)
|
70
|
-
- [`no-wrapper-object-types`](./no-wrapper-object-types.mdx)
|
package/docs/rules/no-shadow.mdx
DELETED
@@ -1,143 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow variable declarations from shadowing variables declared in the outer scope.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-shadow** for documentation.
|
11
|
-
|
12
|
-
It adds support for TypeScript's `this` parameters and global augmentation, and adds options for TypeScript features.
|
13
|
-
|
14
|
-
## Options
|
15
|
-
|
16
|
-
This rule adds the following options:
|
17
|
-
|
18
|
-
```ts
|
19
|
-
type AdditionalHoistOptionEntries = 'types' | 'functions-and-types';
|
20
|
-
|
21
|
-
type HoistOptionEntries =
|
22
|
-
| BaseNoShadowHoistOptionEntries
|
23
|
-
| AdditionalHoistOptionEntries;
|
24
|
-
|
25
|
-
interface Options extends BaseNoShadowOptions {
|
26
|
-
hoist?: HoistOptionEntries;
|
27
|
-
ignoreTypeValueShadow?: boolean;
|
28
|
-
ignoreFunctionTypeParameterNameValueShadow?: boolean;
|
29
|
-
}
|
30
|
-
|
31
|
-
const defaultOptions: Options = {
|
32
|
-
...baseNoShadowDefaultOptions,
|
33
|
-
hoist: 'functions-and-types',
|
34
|
-
ignoreTypeValueShadow: true,
|
35
|
-
ignoreFunctionTypeParameterNameValueShadow: true,
|
36
|
-
};
|
37
|
-
```
|
38
|
-
|
39
|
-
### hoist: `types`
|
40
|
-
|
41
|
-
Examples of incorrect code for the `{ "hoist": "types" }` option:
|
42
|
-
|
43
|
-
```ts option='{ "hoist": "types" }' showPlaygroundButton
|
44
|
-
type Bar<Foo> = 1;
|
45
|
-
type Foo = 1;
|
46
|
-
```
|
47
|
-
|
48
|
-
### hoist: `functions-and-types`
|
49
|
-
|
50
|
-
Examples of incorrect code for the `{ "hoist": "functions-and-types" }` option:
|
51
|
-
|
52
|
-
```ts option='{ "hoist": "functions-and-types" }' showPlaygroundButton
|
53
|
-
// types
|
54
|
-
type Bar<Foo> = 1;
|
55
|
-
type Foo = 1;
|
56
|
-
|
57
|
-
// functions
|
58
|
-
if (true) {
|
59
|
-
let b = 6;
|
60
|
-
}
|
61
|
-
|
62
|
-
function b() {}
|
63
|
-
```
|
64
|
-
|
65
|
-
### `ignoreTypeValueShadow`
|
66
|
-
|
67
|
-
{/* insert option description */}
|
68
|
-
|
69
|
-
This is generally safe because you cannot use variables in type locations without a `typeof` operator, so there's little risk of confusion.
|
70
|
-
|
71
|
-
Examples of **correct** code with `{ ignoreTypeValueShadow: true }`:
|
72
|
-
|
73
|
-
```ts option='{ "ignoreTypeValueShadow": true }' showPlaygroundButton
|
74
|
-
type Foo = number;
|
75
|
-
interface Bar {
|
76
|
-
prop: number;
|
77
|
-
}
|
78
|
-
|
79
|
-
function f() {
|
80
|
-
const Foo = 1;
|
81
|
-
const Bar = 'test';
|
82
|
-
}
|
83
|
-
```
|
84
|
-
|
85
|
-
:::note
|
86
|
-
|
87
|
-
_Shadowing_ specifically refers to two identical identifiers that are in different, nested scopes. This is different from _redeclaration_, which is when two identical identifiers are in the same scope. Redeclaration is covered by the [`no-redeclare`](./no-redeclare.mdx) rule instead.
|
88
|
-
|
89
|
-
:::
|
90
|
-
|
91
|
-
### `ignoreFunctionTypeParameterNameValueShadow`
|
92
|
-
|
93
|
-
{/* insert option description */}
|
94
|
-
|
95
|
-
Each of a function type's arguments creates a value variable within the scope of the function type. This is done so that you can reference the type later using the `typeof` operator:
|
96
|
-
|
97
|
-
```ts
|
98
|
-
type Func = (test: string) => typeof test;
|
99
|
-
|
100
|
-
declare const fn: Func;
|
101
|
-
const result = fn('str'); // typeof result === string
|
102
|
-
```
|
103
|
-
|
104
|
-
This means that function type arguments shadow value variable names in parent scopes:
|
105
|
-
|
106
|
-
```ts
|
107
|
-
let test = 1;
|
108
|
-
type TestType = typeof test; // === number
|
109
|
-
type Func = (test: string) => typeof test; // this "test" references the argument, not the variable
|
110
|
-
|
111
|
-
declare const fn: Func;
|
112
|
-
const result = fn('str'); // typeof result === string
|
113
|
-
```
|
114
|
-
|
115
|
-
If you do not use the `typeof` operator in a function type return type position, you can safely turn this option on.
|
116
|
-
|
117
|
-
Examples of **correct** code with `{ ignoreFunctionTypeParameterNameValueShadow: true }`:
|
118
|
-
|
119
|
-
```ts option='{ "ignoreFunctionTypeParameterNameValueShadow": true }' showPlaygroundButton
|
120
|
-
const test = 1;
|
121
|
-
type Func = (test: string) => typeof test;
|
122
|
-
```
|
123
|
-
|
124
|
-
## FAQ
|
125
|
-
|
126
|
-
### Why does the rule report on enum members that share the same name as a variable in a parent scope?
|
127
|
-
|
128
|
-
Reporting on this case isn't a bug - it is completely intentional and correct reporting! The rule reports due to a relatively unknown feature of enums - enum members create a variable within the enum scope so that they can be referenced within the enum without a qualifier.
|
129
|
-
|
130
|
-
To illustrate this with an example:
|
131
|
-
|
132
|
-
```ts
|
133
|
-
const A = 2;
|
134
|
-
enum Test {
|
135
|
-
A = 1,
|
136
|
-
B = A,
|
137
|
-
}
|
138
|
-
|
139
|
-
console.log(Test.B);
|
140
|
-
// what should be logged?
|
141
|
-
```
|
142
|
-
|
143
|
-
Naively looking at the above code, it might look like the log should output `2`, because the outer variable `A`'s value is `2` - however, the code instead outputs `1`, which is the value of `Test.A`. This is because the unqualified code `B = A` is equivalent to the fully-qualified code `B = Test.A`. Due to this behavior, the enum member has **shadowed** the outer variable declaration.
|
@@ -1,124 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow aliasing `this`.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-this-alias** for documentation.
|
11
|
-
|
12
|
-
Assigning a variable to `this` instead of properly using arrow lambdas may be a symptom of pre-ES6 practices
|
13
|
-
or not managing scope well.
|
14
|
-
|
15
|
-
## Examples
|
16
|
-
|
17
|
-
<Tabs>
|
18
|
-
<TabItem value="❌ Incorrect">
|
19
|
-
|
20
|
-
```ts
|
21
|
-
const self = this;
|
22
|
-
|
23
|
-
setTimeout(function () {
|
24
|
-
self.doWork();
|
25
|
-
});
|
26
|
-
```
|
27
|
-
|
28
|
-
</TabItem>
|
29
|
-
<TabItem value="✅ Correct">
|
30
|
-
|
31
|
-
```ts
|
32
|
-
setTimeout(() => {
|
33
|
-
this.doWork();
|
34
|
-
});
|
35
|
-
```
|
36
|
-
|
37
|
-
</TabItem>
|
38
|
-
</Tabs>
|
39
|
-
|
40
|
-
## Options
|
41
|
-
|
42
|
-
### `allowDestructuring`
|
43
|
-
|
44
|
-
{/* insert option description */}
|
45
|
-
|
46
|
-
It can sometimes be useful to destructure properties from a class instance, such as retrieving multiple properties from the instance in one of its methods.
|
47
|
-
`allowDestructuring` allows those destructures and is `true` by default.
|
48
|
-
You can explicitly disallow them by setting `allowDestructuring` to `false`.
|
49
|
-
|
50
|
-
Examples of code for the `{ "allowDestructuring": false }` option:
|
51
|
-
|
52
|
-
<Tabs>
|
53
|
-
<TabItem value="❌ Incorrect">
|
54
|
-
|
55
|
-
```ts option='{ "allowDestructuring": false }'
|
56
|
-
class ComponentLike {
|
57
|
-
props: unknown;
|
58
|
-
state: unknown;
|
59
|
-
|
60
|
-
render() {
|
61
|
-
const { props, state } = this;
|
62
|
-
|
63
|
-
console.log(props);
|
64
|
-
console.log(state);
|
65
|
-
}
|
66
|
-
}
|
67
|
-
```
|
68
|
-
|
69
|
-
</TabItem>
|
70
|
-
<TabItem value="✅ Correct">
|
71
|
-
|
72
|
-
```ts option='{ "allowDestructuring": false }'
|
73
|
-
class ComponentLike {
|
74
|
-
props: unknown;
|
75
|
-
state: unknown;
|
76
|
-
|
77
|
-
render() {
|
78
|
-
console.log(this.props);
|
79
|
-
console.log(this.state);
|
80
|
-
}
|
81
|
-
}
|
82
|
-
```
|
83
|
-
|
84
|
-
</TabItem>
|
85
|
-
</Tabs>
|
86
|
-
|
87
|
-
### `allowedNames`
|
88
|
-
|
89
|
-
{/* insert option description */}
|
90
|
-
|
91
|
-
`no-this-alias` can alternately be used to allow only a specific list of names as `this` aliases.
|
92
|
-
We recommend against this except as a transitory step towards fixing all rule violations.
|
93
|
-
|
94
|
-
Examples of code for the `{ "allowedNames": ["self"] }` option:
|
95
|
-
|
96
|
-
<Tabs>
|
97
|
-
<TabItem value="❌ Incorrect">
|
98
|
-
|
99
|
-
```ts option='{ "allowedNames": ["self"] }'
|
100
|
-
class Example {
|
101
|
-
method() {
|
102
|
-
const that = this;
|
103
|
-
}
|
104
|
-
}
|
105
|
-
```
|
106
|
-
|
107
|
-
</TabItem>
|
108
|
-
<TabItem value="✅ Correct">
|
109
|
-
|
110
|
-
```ts option='{ "allowedNames": ["self"] }'
|
111
|
-
class Example {
|
112
|
-
method() {
|
113
|
-
const self = this;
|
114
|
-
}
|
115
|
-
}
|
116
|
-
```
|
117
|
-
|
118
|
-
</TabItem>
|
119
|
-
</Tabs>
|
120
|
-
|
121
|
-
## When Not To Use It
|
122
|
-
|
123
|
-
If your project is structured in a way that it needs to assign `this` to variables, this rule is likely not for you.
|
124
|
-
If only a subset of your project assigns `this` to variables then you might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
|