@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
package/docs/rules/typedef.mdx
DELETED
@@ -1,350 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Require type annotations in certain places.'
|
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/typedef** for documentation.
|
11
|
-
|
12
|
-
:::caution
|
13
|
-
|
14
|
-
This is an old, deprecated rule.
|
15
|
-
It will be removed in a future major version of typescript-eslint.
|
16
|
-
|
17
|
-
Requiring type annotations unnecessarily can be cumbersome to maintain and generally reduces code readability.
|
18
|
-
TypeScript is often better at inferring types than easily written type annotations would allow.
|
19
|
-
|
20
|
-
**Instead of enabling `typedef`, it is generally recommended to use the `--noImplicitAny` and `--strictPropertyInitialization` compiler options to enforce type annotations only when useful.**
|
21
|
-
|
22
|
-
:::
|
23
|
-
|
24
|
-
TypeScript cannot always infer types for all places in code.
|
25
|
-
Some locations require type annotations for their types to be inferred.
|
26
|
-
|
27
|
-
This rule can enforce type annotations in locations regardless of whether they're required.
|
28
|
-
This is typically used to maintain consistency for element types that sometimes require them.
|
29
|
-
|
30
|
-
```ts
|
31
|
-
class ContainsText {
|
32
|
-
// There must be a type annotation here to infer the type
|
33
|
-
delayedText: string;
|
34
|
-
|
35
|
-
// `typedef` requires a type annotation here to maintain consistency
|
36
|
-
immediateTextExplicit: string = 'text';
|
37
|
-
|
38
|
-
// This is still a string type because of its initial value
|
39
|
-
immediateTextImplicit = 'text';
|
40
|
-
}
|
41
|
-
```
|
42
|
-
|
43
|
-
> To enforce type definitions existing on call signatures, use [`explicit-function-return-type`](./explicit-function-return-type.mdx), or [`explicit-module-boundary-types`](./explicit-module-boundary-types.mdx).
|
44
|
-
|
45
|
-
## Options
|
46
|
-
|
47
|
-
For example, with the following configuration:
|
48
|
-
|
49
|
-
```json
|
50
|
-
{
|
51
|
-
"rules": {
|
52
|
-
"@typescript-eslint/typedef": [
|
53
|
-
"error",
|
54
|
-
{
|
55
|
-
"arrowParameter": true,
|
56
|
-
"variableDeclaration": true
|
57
|
-
}
|
58
|
-
]
|
59
|
-
}
|
60
|
-
}
|
61
|
-
```
|
62
|
-
|
63
|
-
- Type annotations on arrow function parameters are required
|
64
|
-
- Type annotations on variables are required
|
65
|
-
|
66
|
-
### `arrayDestructuring`
|
67
|
-
|
68
|
-
{/* insert option description */}
|
69
|
-
|
70
|
-
Examples of code with `{ "arrayDestructuring": true }`:
|
71
|
-
|
72
|
-
<Tabs>
|
73
|
-
<TabItem value="❌ Incorrect">
|
74
|
-
|
75
|
-
```ts option='{ "arrayDestructuring": true }'
|
76
|
-
const [a] = [1];
|
77
|
-
const [b, c] = [1, 2];
|
78
|
-
```
|
79
|
-
|
80
|
-
</TabItem>
|
81
|
-
<TabItem value="✅ Correct">
|
82
|
-
|
83
|
-
```ts option='{ "arrayDestructuring": true }'
|
84
|
-
const [a]: number[] = [1];
|
85
|
-
const [b]: [number] = [2];
|
86
|
-
const [c, d]: [boolean, string] = [true, 'text'];
|
87
|
-
|
88
|
-
for (const [key, val] of new Map([['key', 1]])) {
|
89
|
-
}
|
90
|
-
```
|
91
|
-
|
92
|
-
</TabItem>
|
93
|
-
</Tabs>
|
94
|
-
|
95
|
-
### `arrowParameter`
|
96
|
-
|
97
|
-
{/* insert option description */}
|
98
|
-
|
99
|
-
Examples of code with `{ "arrowParameter": true }`:
|
100
|
-
|
101
|
-
<Tabs>
|
102
|
-
<TabItem value="❌ Incorrect">
|
103
|
-
|
104
|
-
```ts option='{ "arrowParameter": true }'
|
105
|
-
const logsSize = size => console.log(size);
|
106
|
-
|
107
|
-
['hello', 'world'].map(text => text.length);
|
108
|
-
|
109
|
-
const mapper = {
|
110
|
-
map: text => text + '...',
|
111
|
-
};
|
112
|
-
```
|
113
|
-
|
114
|
-
</TabItem>
|
115
|
-
<TabItem value="✅ Correct">
|
116
|
-
|
117
|
-
```ts option='{ "arrowParameter": true }'
|
118
|
-
const logsSize = (size: number) => console.log(size);
|
119
|
-
|
120
|
-
['hello', 'world'].map((text: string) => text.length);
|
121
|
-
|
122
|
-
const mapper = {
|
123
|
-
map: (text: string) => text + '...',
|
124
|
-
};
|
125
|
-
```
|
126
|
-
|
127
|
-
</TabItem>
|
128
|
-
</Tabs>
|
129
|
-
|
130
|
-
### `memberVariableDeclaration`
|
131
|
-
|
132
|
-
{/* insert option description */}
|
133
|
-
|
134
|
-
Examples of code with `{ "memberVariableDeclaration": true }`:
|
135
|
-
|
136
|
-
<Tabs>
|
137
|
-
<TabItem value="❌ Incorrect">
|
138
|
-
|
139
|
-
```ts option='{ "memberVariableDeclaration": true }'
|
140
|
-
class ContainsText {
|
141
|
-
delayedText;
|
142
|
-
immediateTextImplicit = 'text';
|
143
|
-
}
|
144
|
-
```
|
145
|
-
|
146
|
-
</TabItem>
|
147
|
-
<TabItem value="✅ Correct">
|
148
|
-
|
149
|
-
```ts option='{ "memberVariableDeclaration": true }'
|
150
|
-
class ContainsText {
|
151
|
-
delayedText: string;
|
152
|
-
immediateTextImplicit: string = 'text';
|
153
|
-
}
|
154
|
-
```
|
155
|
-
|
156
|
-
</TabItem>
|
157
|
-
</Tabs>
|
158
|
-
|
159
|
-
### `objectDestructuring`
|
160
|
-
|
161
|
-
{/* insert option description */}
|
162
|
-
|
163
|
-
Examples of code with `{ "objectDestructuring": true }`:
|
164
|
-
|
165
|
-
<Tabs>
|
166
|
-
<TabItem value="❌ Incorrect">
|
167
|
-
|
168
|
-
```ts option='{ "objectDestructuring": true }'
|
169
|
-
const { length } = 'text';
|
170
|
-
const [b, c] = Math.random() ? [1, 2] : [3, 4];
|
171
|
-
```
|
172
|
-
|
173
|
-
</TabItem>
|
174
|
-
<TabItem value="✅ Correct">
|
175
|
-
|
176
|
-
```ts option='{ "objectDestructuring": true }'
|
177
|
-
const { length }: { length: number } = 'text';
|
178
|
-
const [b, c]: [number, number] = Math.random() ? [1, 2] : [3, 4];
|
179
|
-
|
180
|
-
for (const { key, val } of [{ key: 'key', val: 1 }]) {
|
181
|
-
}
|
182
|
-
```
|
183
|
-
|
184
|
-
</TabItem>
|
185
|
-
</Tabs>
|
186
|
-
|
187
|
-
### `parameter`
|
188
|
-
|
189
|
-
{/* insert option description */}
|
190
|
-
|
191
|
-
Examples of code with `{ "parameter": true }`:
|
192
|
-
|
193
|
-
<Tabs>
|
194
|
-
<TabItem value="❌ Incorrect">
|
195
|
-
|
196
|
-
```ts option='{ "parameter": true }'
|
197
|
-
function logsSize(size): void {
|
198
|
-
console.log(size);
|
199
|
-
}
|
200
|
-
|
201
|
-
const doublesSize = function (size): number {
|
202
|
-
return size * 2;
|
203
|
-
};
|
204
|
-
|
205
|
-
const divider = {
|
206
|
-
curriesSize(size): number {
|
207
|
-
return size;
|
208
|
-
},
|
209
|
-
dividesSize: function (size): number {
|
210
|
-
return size / 2;
|
211
|
-
},
|
212
|
-
};
|
213
|
-
|
214
|
-
class Logger {
|
215
|
-
log(text): boolean {
|
216
|
-
console.log('>', text);
|
217
|
-
return true;
|
218
|
-
}
|
219
|
-
}
|
220
|
-
```
|
221
|
-
|
222
|
-
</TabItem>
|
223
|
-
<TabItem value="✅ Correct">
|
224
|
-
|
225
|
-
```ts option='{ "parameter": true }'
|
226
|
-
function logsSize(size: number): void {
|
227
|
-
console.log(size);
|
228
|
-
}
|
229
|
-
|
230
|
-
const doublesSize = function (size: number): number {
|
231
|
-
return size * 2;
|
232
|
-
};
|
233
|
-
|
234
|
-
const divider = {
|
235
|
-
curriesSize(size: number): number {
|
236
|
-
return size;
|
237
|
-
},
|
238
|
-
dividesSize: function (size: number): number {
|
239
|
-
return size / 2;
|
240
|
-
},
|
241
|
-
};
|
242
|
-
|
243
|
-
class Logger {
|
244
|
-
log(text: boolean): boolean {
|
245
|
-
console.log('>', text);
|
246
|
-
return true;
|
247
|
-
}
|
248
|
-
}
|
249
|
-
```
|
250
|
-
|
251
|
-
</TabItem>
|
252
|
-
</Tabs>
|
253
|
-
|
254
|
-
### `propertyDeclaration`
|
255
|
-
|
256
|
-
{/* insert option description */}
|
257
|
-
|
258
|
-
Examples of code with `{ "propertyDeclaration": true }`:
|
259
|
-
|
260
|
-
<Tabs>
|
261
|
-
<TabItem value="❌ Incorrect">
|
262
|
-
|
263
|
-
```ts option='{ "propertyDeclaration": true }'
|
264
|
-
type Members = {
|
265
|
-
member;
|
266
|
-
otherMember;
|
267
|
-
};
|
268
|
-
```
|
269
|
-
|
270
|
-
</TabItem>
|
271
|
-
<TabItem value="✅ Correct">
|
272
|
-
|
273
|
-
```ts option='{ "propertyDeclaration": true }'
|
274
|
-
type Members = {
|
275
|
-
member: boolean;
|
276
|
-
otherMember: string;
|
277
|
-
};
|
278
|
-
```
|
279
|
-
|
280
|
-
</TabItem>
|
281
|
-
</Tabs>
|
282
|
-
|
283
|
-
### `variableDeclaration`
|
284
|
-
|
285
|
-
{/* insert option description */}
|
286
|
-
|
287
|
-
Examples of code with `{ "variableDeclaration": true }`:
|
288
|
-
|
289
|
-
<Tabs>
|
290
|
-
<TabItem value="❌ Incorrect">
|
291
|
-
|
292
|
-
```ts option='{ "variableDeclaration": true }'
|
293
|
-
const text = 'text';
|
294
|
-
let initialText = 'text';
|
295
|
-
let delayedText;
|
296
|
-
```
|
297
|
-
|
298
|
-
</TabItem>
|
299
|
-
<TabItem value="✅ Correct">
|
300
|
-
|
301
|
-
```ts option='{ "variableDeclaration": true }'
|
302
|
-
const text: string = 'text';
|
303
|
-
let initialText: string = 'text';
|
304
|
-
let delayedText: string;
|
305
|
-
```
|
306
|
-
|
307
|
-
</TabItem>
|
308
|
-
</Tabs>
|
309
|
-
|
310
|
-
### `variableDeclarationIgnoreFunction`
|
311
|
-
|
312
|
-
{/* insert option description */}
|
313
|
-
|
314
|
-
Examples of code with `{ "variableDeclaration": true, "variableDeclarationIgnoreFunction": true }`:
|
315
|
-
|
316
|
-
<Tabs>
|
317
|
-
<TabItem value="❌ Incorrect">
|
318
|
-
|
319
|
-
```ts option='{ "variableDeclaration": true, "variableDeclarationIgnoreFunction": true }'
|
320
|
-
const text = 'text';
|
321
|
-
```
|
322
|
-
|
323
|
-
</TabItem>
|
324
|
-
<TabItem value="✅ Correct">
|
325
|
-
|
326
|
-
```ts option='{ "variableDeclaration": true, "variableDeclarationIgnoreFunction": true }'
|
327
|
-
const a = (): void => {};
|
328
|
-
const b = function (): void {};
|
329
|
-
const c: () => void = (): void => {};
|
330
|
-
|
331
|
-
class Foo {
|
332
|
-
a = (): void => {};
|
333
|
-
b = function (): void {};
|
334
|
-
c: () => void = (): void => {};
|
335
|
-
}
|
336
|
-
```
|
337
|
-
|
338
|
-
</TabItem>
|
339
|
-
</Tabs>
|
340
|
-
|
341
|
-
## When Not To Use It
|
342
|
-
|
343
|
-
If you are using stricter TypeScript compiler options, particularly `--noImplicitAny` and/or `--strictPropertyInitialization`, you likely don't need this rule.
|
344
|
-
|
345
|
-
In general, if you do not consider the cost of writing unnecessary type annotations reasonable, then do not use this rule.
|
346
|
-
|
347
|
-
## Further Reading
|
348
|
-
|
349
|
-
- [TypeScript Type System](https://basarat.gitbooks.io/typescript/docs/types/type-system.html)
|
350
|
-
- [Type Inference](https://www.typescriptlang.org/docs/handbook/type-inference.html)
|
@@ -1,114 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Enforce unbound methods are called with their expected 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/unbound-method** for documentation.
|
11
|
-
|
12
|
-
Class method functions don't preserve the class scope when passed as standalone variables ("unbound").
|
13
|
-
If your function does not access `this`, [you can annotate it with `this: void`](https://www.typescriptlang.org/docs/handbook/2/functions.html#declaring-this-in-a-function), or consider using an arrow function instead.
|
14
|
-
Otherwise, passing class methods around as values can remove type safety by failing to capture `this`.
|
15
|
-
|
16
|
-
This rule reports when a class method is referenced in an unbound manner.
|
17
|
-
|
18
|
-
:::note Tip
|
19
|
-
If you're working with `jest`, you can use [`eslint-plugin-jest`'s version of this rule](https://github.com/jest-community/eslint-plugin-jest/blob/main/docs/rules/unbound-method.md) to lint your test files, which knows when it's ok to pass an unbound method to `expect` calls.
|
20
|
-
:::
|
21
|
-
|
22
|
-
## Examples
|
23
|
-
|
24
|
-
<Tabs>
|
25
|
-
<TabItem value="❌ Incorrect">
|
26
|
-
|
27
|
-
```ts
|
28
|
-
class MyClass {
|
29
|
-
public log(): void {
|
30
|
-
console.log(this);
|
31
|
-
}
|
32
|
-
}
|
33
|
-
|
34
|
-
const instance = new MyClass();
|
35
|
-
|
36
|
-
// This logs the global scope (`window`/`global`), not the class instance
|
37
|
-
const myLog = instance.log;
|
38
|
-
myLog();
|
39
|
-
|
40
|
-
// This log might later be called with an incorrect scope
|
41
|
-
const { log } = instance;
|
42
|
-
|
43
|
-
// arith.double may refer to `this` internally
|
44
|
-
const arith = {
|
45
|
-
double(x: number): number {
|
46
|
-
return x * 2;
|
47
|
-
},
|
48
|
-
};
|
49
|
-
const { double } = arith;
|
50
|
-
```
|
51
|
-
|
52
|
-
</TabItem>
|
53
|
-
<TabItem value="✅ Correct">
|
54
|
-
|
55
|
-
```ts
|
56
|
-
class MyClass {
|
57
|
-
public logUnbound(): void {
|
58
|
-
console.log(this);
|
59
|
-
}
|
60
|
-
|
61
|
-
public logBound = () => console.log(this);
|
62
|
-
}
|
63
|
-
|
64
|
-
const instance = new MyClass();
|
65
|
-
|
66
|
-
// logBound will always be bound with the correct scope
|
67
|
-
const { logBound } = instance;
|
68
|
-
logBound();
|
69
|
-
|
70
|
-
// .bind and lambdas will also add a correct scope
|
71
|
-
const dotBindLog = instance.logUnbound.bind(instance);
|
72
|
-
const innerLog = () => instance.logUnbound();
|
73
|
-
|
74
|
-
// arith.double explicitly declares that it does not refer to `this` internally
|
75
|
-
const arith = {
|
76
|
-
double(this: void, x: number): number {
|
77
|
-
return x * 2;
|
78
|
-
},
|
79
|
-
};
|
80
|
-
const { double } = arith;
|
81
|
-
```
|
82
|
-
|
83
|
-
</TabItem>
|
84
|
-
</Tabs>
|
85
|
-
|
86
|
-
## Options
|
87
|
-
|
88
|
-
### `ignoreStatic`
|
89
|
-
|
90
|
-
{/* insert option description */}
|
91
|
-
|
92
|
-
Examples of **correct** code for this rule with `{ ignoreStatic: true }`:
|
93
|
-
|
94
|
-
```ts option='{ "ignoreStatic": true }' showPlaygroundButton
|
95
|
-
class OtherClass {
|
96
|
-
static log() {
|
97
|
-
console.log(OtherClass);
|
98
|
-
}
|
99
|
-
}
|
100
|
-
|
101
|
-
// With `ignoreStatic`, statics are assumed to not rely on a particular scope
|
102
|
-
const { log } = OtherClass;
|
103
|
-
|
104
|
-
log();
|
105
|
-
```
|
106
|
-
|
107
|
-
## When Not To Use It
|
108
|
-
|
109
|
-
If your project dynamically changes `this` scopes around in a way TypeScript has difficulties modeling, this rule may not be viable to use.
|
110
|
-
For example, some functions have an additional parameter for specifying the `this` context, such as `Reflect.apply`, and array methods like `Array.prototype.map`.
|
111
|
-
This semantic is not easily expressed by TypeScript.
|
112
|
-
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.
|
113
|
-
|
114
|
-
If you're wanting to use `toBeCalled` and similar matches in `jest` tests, you can disable this rule for your test files in favor of [`eslint-plugin-jest`'s version of this rule](https://github.com/jest-community/eslint-plugin-jest/blob/main/docs/rules/unbound-method.mdx).
|
@@ -1,132 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow two overloads that could be unified into one with a union or an optional/rest parameter.'
|
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/unified-signatures** for documentation.
|
11
|
-
|
12
|
-
Function overload signatures are a TypeScript way to define a function that can be called in multiple very different ways.
|
13
|
-
Overload signatures add syntax and theoretical bloat, so it's generally best to avoid using them when possible.
|
14
|
-
Switching to union types and/or optional or rest parameters can often avoid the need for overload signatures.
|
15
|
-
|
16
|
-
This rule reports when function overload signatures can be replaced by a single function signature.
|
17
|
-
|
18
|
-
## Examples
|
19
|
-
|
20
|
-
<Tabs>
|
21
|
-
<TabItem value="❌ Incorrect">
|
22
|
-
|
23
|
-
```ts
|
24
|
-
function x(x: number): void;
|
25
|
-
function x(x: string): void;
|
26
|
-
```
|
27
|
-
|
28
|
-
```ts
|
29
|
-
function y(): void;
|
30
|
-
function y(...x: number[]): void;
|
31
|
-
```
|
32
|
-
|
33
|
-
</TabItem>
|
34
|
-
<TabItem value="✅ Correct">
|
35
|
-
|
36
|
-
```ts
|
37
|
-
function x(x: number | string): void;
|
38
|
-
```
|
39
|
-
|
40
|
-
```ts
|
41
|
-
function y(...x: number[]): void;
|
42
|
-
```
|
43
|
-
|
44
|
-
```ts
|
45
|
-
// This rule won't check overload signatures with different rest parameter types.
|
46
|
-
// See https://github.com/microsoft/TypeScript/issues/5077
|
47
|
-
function f(...a: number[]): void;
|
48
|
-
function f(...a: string[]): void;
|
49
|
-
```
|
50
|
-
|
51
|
-
</TabItem>
|
52
|
-
</Tabs>
|
53
|
-
|
54
|
-
## Options
|
55
|
-
|
56
|
-
### `ignoreDifferentlyNamedParameters`
|
57
|
-
|
58
|
-
{/* insert option description */}
|
59
|
-
|
60
|
-
Examples of code for this rule with `ignoreDifferentlyNamedParameters`:
|
61
|
-
|
62
|
-
<Tabs>
|
63
|
-
<TabItem value="❌ Incorrect">
|
64
|
-
|
65
|
-
```ts option='{ "ignoreDifferentlyNamedParameters": true }'
|
66
|
-
function f(a: number): void;
|
67
|
-
function f(a: string): void;
|
68
|
-
```
|
69
|
-
|
70
|
-
</TabItem>
|
71
|
-
<TabItem value="✅ Correct">
|
72
|
-
|
73
|
-
```ts option='{ "ignoreDifferentlyNamedParameters": true }'
|
74
|
-
function f(a: number): void;
|
75
|
-
function f(b: string): void;
|
76
|
-
```
|
77
|
-
|
78
|
-
</TabItem>
|
79
|
-
</Tabs>
|
80
|
-
|
81
|
-
### `ignoreOverloadsWithDifferentJSDoc`
|
82
|
-
|
83
|
-
{/* insert option description */}
|
84
|
-
|
85
|
-
Examples of code for this rule with `ignoreOverloadsWithDifferentJSDoc`:
|
86
|
-
|
87
|
-
<Tabs>
|
88
|
-
<TabItem value="❌ Incorrect">
|
89
|
-
|
90
|
-
```ts option='{ "ignoreOverloadsWithDifferentJSDoc": true }'
|
91
|
-
declare function f(x: string): void;
|
92
|
-
declare function f(x: boolean): void;
|
93
|
-
/**
|
94
|
-
* @deprecate
|
95
|
-
*/
|
96
|
-
declare function f(x: number): void;
|
97
|
-
/**
|
98
|
-
* @deprecate
|
99
|
-
*/
|
100
|
-
declare function f(x: null): void;
|
101
|
-
```
|
102
|
-
|
103
|
-
</TabItem>
|
104
|
-
<TabItem value="✅ Correct">
|
105
|
-
|
106
|
-
```ts option='{ "ignoreOverloadsWithDifferentJSDoc": true }'
|
107
|
-
declare function f(x: string): void;
|
108
|
-
/**
|
109
|
-
* This signature does something else.
|
110
|
-
*/
|
111
|
-
declare function f(x: boolean): void;
|
112
|
-
/**
|
113
|
-
* @async
|
114
|
-
*/
|
115
|
-
declare function f(x: number): void;
|
116
|
-
/**
|
117
|
-
* @deprecate
|
118
|
-
*/
|
119
|
-
declare function f(x: null): void;
|
120
|
-
```
|
121
|
-
|
122
|
-
</TabItem>
|
123
|
-
</Tabs>
|
124
|
-
|
125
|
-
## When Not To Use It
|
126
|
-
|
127
|
-
This is purely a stylistic rule to help with readability of function signature overloads.
|
128
|
-
You can turn it off if you don't want to consistently keep them next to each other and unified.
|
129
|
-
|
130
|
-
## Related To
|
131
|
-
|
132
|
-
- [`adjacent-overload-signatures`](./adjacent-overload-signatures.mdx)
|
@@ -1,97 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Enforce typing arguments in Promise rejection callbacks as `unknown`.'
|
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/use-unknown-in-catch-callback-variable** for documentation.
|
11
|
-
|
12
|
-
This rule enforces that you always use the `unknown` type for the parameter of a Promise rejection callback.
|
13
|
-
|
14
|
-
<Tabs>
|
15
|
-
<TabItem value="❌ Incorrect">
|
16
|
-
|
17
|
-
```ts
|
18
|
-
Promise.reject(new Error('I will reject!')).catch(err => {
|
19
|
-
console.log(err);
|
20
|
-
});
|
21
|
-
|
22
|
-
Promise.reject(new Error('I will reject!')).catch((err: any) => {
|
23
|
-
console.log(err);
|
24
|
-
});
|
25
|
-
|
26
|
-
Promise.reject(new Error('I will reject!')).catch((err: Error) => {
|
27
|
-
console.log(err);
|
28
|
-
});
|
29
|
-
|
30
|
-
Promise.reject(new Error('I will reject!')).then(
|
31
|
-
result => {
|
32
|
-
console.log(result);
|
33
|
-
},
|
34
|
-
err => {
|
35
|
-
console.log(err);
|
36
|
-
},
|
37
|
-
);
|
38
|
-
```
|
39
|
-
|
40
|
-
</TabItem>
|
41
|
-
<TabItem value="✅ Correct">
|
42
|
-
|
43
|
-
```ts
|
44
|
-
Promise.reject(new Error('I will reject!')).catch((err: unknown) => {
|
45
|
-
console.log(err);
|
46
|
-
});
|
47
|
-
```
|
48
|
-
|
49
|
-
</TabItem>
|
50
|
-
</Tabs>
|
51
|
-
|
52
|
-
The reason for this rule is to enable programmers to impose constraints on `Promise` error handling analogously to what TypeScript provides for ordinary exception handling.
|
53
|
-
|
54
|
-
For ordinary exceptions, TypeScript treats the `catch` variable as `any` by default. However, `unknown` would be a more accurate type, so TypeScript [introduced the `useUnknownInCatchVariables` compiler option](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-4.html#defaulting-to-the-unknown-type-in-catch-variables---useunknownincatchvariables) to treat the `catch` variable as `unknown` instead.
|
55
|
-
|
56
|
-
```ts
|
57
|
-
try {
|
58
|
-
throw x;
|
59
|
-
} catch (err) {
|
60
|
-
// err has type 'any' with useUnknownInCatchVariables: false
|
61
|
-
// err has type 'unknown' with useUnknownInCatchVariables: true
|
62
|
-
}
|
63
|
-
```
|
64
|
-
|
65
|
-
The Promise analog of the `try-catch` block, [`Promise.prototype.catch()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch), is not affected by the `useUnknownInCatchVariables` compiler option, and its "`catch` variable" will always have the type `any`.
|
66
|
-
|
67
|
-
```ts
|
68
|
-
Promise.reject(x).catch(err => {
|
69
|
-
// err has type 'any' regardless of `useUnknownInCatchVariables`
|
70
|
-
});
|
71
|
-
```
|
72
|
-
|
73
|
-
However, you can still provide an explicit type annotation, which lets you achieve the same effect as the `useUnknownInCatchVariables` option does for synchronous `catch` variables.
|
74
|
-
|
75
|
-
```ts
|
76
|
-
Promise.reject(x).catch((err: unknown) => {
|
77
|
-
// err has type 'unknown'
|
78
|
-
});
|
79
|
-
```
|
80
|
-
|
81
|
-
:::info
|
82
|
-
There is actually a way to have the `catch()` and `then()` callback variables use the `unknown` type _without_ an explicit type annotation at the call sites, but it has the drawback that it involves overriding global type declarations.
|
83
|
-
For example, the library [better-TypeScript-lib](https://github.com/uhyo/better-typescript-lib) sets this up globally for your project (see [the relevant lines in the better-TypeScript-lib source code](https://github.com/uhyo/better-typescript-lib/blob/c294e177d1cc2b1d1803febf8192a4c83a1fe028/lib/lib.es5.d.ts#L635) for details on how).
|
84
|
-
|
85
|
-
For further reading on this, you may also want to look into
|
86
|
-
[the discussion in the proposal for this rule](https://github.com/typescript-eslint/typescript-eslint/issues/7526#issuecomment-1690600813) and [this TypeScript issue on typing catch callback variables as unknown](https://github.com/microsoft/TypeScript/issues/45602).
|
87
|
-
:::
|
88
|
-
|
89
|
-
## When Not To Use It
|
90
|
-
|
91
|
-
If your codebase is not yet able to enable `useUnknownInCatchVariables`, it likely would be similarly difficult to enable this rule.
|
92
|
-
|
93
|
-
If you have modified the global type declarations in order to make `then()` and `catch()` callbacks use the `unknown` type without an explicit type annotation, you do not need this rule.
|
94
|
-
|
95
|
-
## Related To
|
96
|
-
|
97
|
-
- [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
|