@typescript-eslint/eslint-plugin 6.15.0 → 6.17.0
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/dist/configs/all.js +0 -40
- package/dist/configs/all.js.map +1 -1
- package/dist/rules/block-spacing.js +2 -0
- package/dist/rules/block-spacing.js.map +1 -1
- package/dist/rules/brace-style.js +2 -0
- package/dist/rules/brace-style.js.map +1 -1
- package/dist/rules/comma-dangle.js +2 -0
- package/dist/rules/comma-dangle.js.map +1 -1
- package/dist/rules/comma-spacing.js +2 -0
- package/dist/rules/comma-spacing.js.map +1 -1
- package/dist/rules/func-call-spacing.js +2 -0
- package/dist/rules/func-call-spacing.js.map +1 -1
- package/dist/rules/indent.js +2 -0
- package/dist/rules/indent.js.map +1 -1
- package/dist/rules/key-spacing.js +2 -0
- package/dist/rules/key-spacing.js.map +1 -1
- package/dist/rules/keyword-spacing.js +2 -0
- package/dist/rules/keyword-spacing.js.map +1 -1
- package/dist/rules/lines-around-comment.js +2 -0
- package/dist/rules/lines-around-comment.js.map +1 -1
- package/dist/rules/lines-between-class-members.js +2 -0
- package/dist/rules/lines-between-class-members.js.map +1 -1
- package/dist/rules/member-delimiter-style.js +2 -0
- package/dist/rules/member-delimiter-style.js.map +1 -1
- package/dist/rules/no-extra-parens.js +2 -0
- package/dist/rules/no-extra-parens.js.map +1 -1
- package/dist/rules/no-extra-semi.js +2 -0
- package/dist/rules/no-extra-semi.js.map +1 -1
- package/dist/rules/no-floating-promises.js +44 -5
- package/dist/rules/no-floating-promises.js.map +1 -1
- package/dist/rules/no-for-in-array.js +1 -1
- package/dist/rules/no-for-in-array.js.map +1 -1
- package/dist/rules/no-restricted-imports.js +10 -1
- package/dist/rules/no-restricted-imports.js.map +1 -1
- package/dist/rules/object-curly-spacing.js +2 -0
- package/dist/rules/object-curly-spacing.js.map +1 -1
- package/dist/rules/padding-line-between-statements.js +2 -0
- package/dist/rules/padding-line-between-statements.js.map +1 -1
- package/dist/rules/quotes.js +2 -0
- package/dist/rules/quotes.js.map +1 -1
- package/dist/rules/semi.js +2 -0
- package/dist/rules/semi.js.map +1 -1
- package/dist/rules/space-before-blocks.js +2 -0
- package/dist/rules/space-before-blocks.js.map +1 -1
- package/dist/rules/space-before-function-paren.js +2 -0
- package/dist/rules/space-before-function-paren.js.map +1 -1
- package/dist/rules/space-infix-ops.js +2 -0
- package/dist/rules/space-infix-ops.js.map +1 -1
- package/dist/rules/switch-exhaustiveness-check.js +107 -56
- package/dist/rules/switch-exhaustiveness-check.js.map +1 -1
- package/dist/rules/type-annotation-spacing.js +2 -0
- package/dist/rules/type-annotation-spacing.js.map +1 -1
- package/dist/rules/unbound-method.js +13 -51
- package/dist/rules/unbound-method.js.map +1 -1
- package/docs/rules/explicit-member-accessibility.md +2 -2
- package/docs/rules/explicit-module-boundary-types.md +10 -15
- package/docs/rules/no-empty-interface.md +2 -13
- package/docs/rules/no-explicit-any.md +7 -0
- package/docs/rules/no-floating-promises.md +15 -0
- package/docs/rules/no-for-in-array.md +8 -4
- package/docs/rules/no-meaningless-void-operator.md +2 -0
- package/docs/rules/no-misused-promises.md +3 -3
- package/docs/rules/no-parameter-properties.md +12 -0
- package/docs/rules/no-shadow.md +13 -7
- package/docs/rules/no-this-alias.md +69 -0
- package/docs/rules/no-unnecessary-condition.md +1 -2
- package/docs/rules/parameter-properties.md +2 -2
- package/docs/rules/prefer-literal-enum-member.md +3 -1
- package/docs/rules/prefer-nullish-coalescing.md +10 -0
- package/docs/rules/prefer-readonly-parameter-types.md +8 -5
- package/docs/rules/promise-function-async.md +68 -0
- package/docs/rules/sort-type-constituents.md +56 -0
- package/docs/rules/switch-exhaustiveness-check.md +41 -23
- package/docs/rules/triple-slash-reference.md +71 -23
- package/package.json +8 -8
|
@@ -17,7 +17,7 @@ See [`no-floating-promises`](./no-floating-promises.md) for detecting unhandled
|
|
|
17
17
|
|
|
18
18
|
## Options
|
|
19
19
|
|
|
20
|
-
### `
|
|
20
|
+
### `checksConditionals`
|
|
21
21
|
|
|
22
22
|
If you don't want to check conditionals, you can configure the rule with `"checksConditionals": false`:
|
|
23
23
|
|
|
@@ -73,7 +73,7 @@ while (await promise) {
|
|
|
73
73
|
|
|
74
74
|
<!--/tabs-->
|
|
75
75
|
|
|
76
|
-
### `
|
|
76
|
+
### `checksVoidReturn`
|
|
77
77
|
|
|
78
78
|
Likewise, if you don't want to check functions that return promises where a void return is
|
|
79
79
|
expected, your configuration will look like this:
|
|
@@ -182,7 +182,7 @@ eventEmitter.on('some-event', () => {
|
|
|
182
182
|
|
|
183
183
|
<!--/tabs-->
|
|
184
184
|
|
|
185
|
-
### `
|
|
185
|
+
### `checksSpreads`
|
|
186
186
|
|
|
187
187
|
If you don't want to check object spreads, you can add this configuration:
|
|
188
188
|
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
:::danger Deprecated
|
|
2
|
+
|
|
3
|
+
This rule has been deprecated in favour of the [`parameter-properties`](https://typescript-eslint.io/rules/parameter-properties/) rule.
|
|
4
|
+
|
|
5
|
+
:::
|
|
6
|
+
|
|
7
|
+
<!--
|
|
8
|
+
This doc file has been left on purpose to help direct people to the replacement rule.
|
|
9
|
+
|
|
10
|
+
Note that there is no actual way to get to this page in the normal navigation,
|
|
11
|
+
so end-users will only be able to get to this page from the search bar.
|
|
12
|
+
-->
|
package/docs/rules/no-shadow.md
CHANGED
|
@@ -28,25 +28,31 @@ const defaultOptions: Options = {
|
|
|
28
28
|
|
|
29
29
|
### `ignoreTypeValueShadow`
|
|
30
30
|
|
|
31
|
-
When set to `true`, the rule will ignore the case when you name a type the same as a variable.
|
|
32
|
-
|
|
33
|
-
TypeScript allows types and variables to shadow one-another. This is generally safe because you cannot use variables in type locations without a `typeof` operator, so there's little risk of confusion.
|
|
31
|
+
When set to `true`, the rule will ignore the case when you name a type the same as a variable. This is generally safe because you cannot use variables in type locations without a `typeof` operator, so there's little risk of confusion.
|
|
34
32
|
|
|
35
33
|
Examples of **correct** code with `{ ignoreTypeValueShadow: true }`:
|
|
36
34
|
|
|
37
35
|
```ts option='{ "ignoreTypeValueShadow": true }' showPlaygroundButton
|
|
38
36
|
type Foo = number;
|
|
39
|
-
const Foo = 1;
|
|
40
|
-
|
|
41
37
|
interface Bar {
|
|
42
38
|
prop: number;
|
|
43
39
|
}
|
|
44
|
-
|
|
40
|
+
|
|
41
|
+
function f() {
|
|
42
|
+
const Foo = 1;
|
|
43
|
+
const Bar = 'test';
|
|
44
|
+
}
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
+
:::note
|
|
48
|
+
|
|
49
|
+
_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.md) rule instead.
|
|
50
|
+
|
|
51
|
+
:::
|
|
52
|
+
|
|
47
53
|
### `ignoreFunctionTypeParameterNameValueShadow`
|
|
48
54
|
|
|
49
|
-
When set to `true`, the rule will ignore the case when you name a function type
|
|
55
|
+
When set to `true`, the rule will ignore the case when you name a parameter in a function type the same as a variable.
|
|
50
56
|
|
|
51
57
|
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:
|
|
52
58
|
|
|
@@ -33,6 +33,75 @@ setTimeout(() => {
|
|
|
33
33
|
|
|
34
34
|
## Options
|
|
35
35
|
|
|
36
|
+
### `allowDestructuring`
|
|
37
|
+
|
|
38
|
+
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.
|
|
39
|
+
`allowDestructuring` allows those destructures and is `true` by default.
|
|
40
|
+
You can explicitly disallow them by setting `allowDestructuring` to `false`.
|
|
41
|
+
|
|
42
|
+
Examples of code for the `{ "allowDestructuring": false }` option:
|
|
43
|
+
|
|
44
|
+
<!--tabs-->
|
|
45
|
+
|
|
46
|
+
#### ❌ Incorrect
|
|
47
|
+
|
|
48
|
+
```ts option='{ "allowDestructuring": false }'
|
|
49
|
+
class ComponentLike {
|
|
50
|
+
props: unknown;
|
|
51
|
+
state: unknown;
|
|
52
|
+
|
|
53
|
+
render() {
|
|
54
|
+
const { props, state } = this;
|
|
55
|
+
|
|
56
|
+
console.log(props);
|
|
57
|
+
console.log(state);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
#### ✅ Correct
|
|
63
|
+
|
|
64
|
+
```ts option='{ "allowDestructuring": false }'
|
|
65
|
+
class ComponentLike {
|
|
66
|
+
props: unknown;
|
|
67
|
+
state: unknown;
|
|
68
|
+
|
|
69
|
+
render() {
|
|
70
|
+
console.log(this.props);
|
|
71
|
+
console.log(this.state);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### `allowedNames`
|
|
77
|
+
|
|
78
|
+
`no-this-alias` can alternately be used to allow only a specific list of names as `this` aliases.
|
|
79
|
+
We recommend against this except as a transitory step towards fixing all rule violations.
|
|
80
|
+
|
|
81
|
+
Examples of code for the `{ "allowedNames": ["self"] }` option:
|
|
82
|
+
|
|
83
|
+
<!--tabs-->
|
|
84
|
+
|
|
85
|
+
#### ❌ Incorrect
|
|
86
|
+
|
|
87
|
+
```ts option='{ "allowedNames": ["self"] }'
|
|
88
|
+
class Example {
|
|
89
|
+
method() {
|
|
90
|
+
const that = this;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### ✅ Correct
|
|
96
|
+
|
|
97
|
+
```ts option='{ "allowedNames": ["self"] }'
|
|
98
|
+
class Example {
|
|
99
|
+
method() {
|
|
100
|
+
const self = this;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
36
105
|
## When Not To Use It
|
|
37
106
|
|
|
38
107
|
If your project is structured in a way that it needs to assign `this` to variables, this rule is likely not for you.
|
|
@@ -101,8 +101,7 @@ You might consider using [ESLint disable comments](https://eslint.org/docs/lates
|
|
|
101
101
|
This rule has a known edge case of triggering on conditions that were modified within function calls (as side effects).
|
|
102
102
|
It is due to limitations of TypeScript's type narrowing.
|
|
103
103
|
See [#9998](https://github.com/microsoft/TypeScript/issues/9998) for details.
|
|
104
|
-
|
|
105
|
-
We recommend upcasting the variable with a [type assertion](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions).
|
|
104
|
+
We recommend using a [type assertion](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions) in those cases.
|
|
106
105
|
|
|
107
106
|
```ts
|
|
108
107
|
let condition = false as boolean;
|
|
@@ -19,7 +19,7 @@ It may take an options object containing either or both of:
|
|
|
19
19
|
- `"allow"`: allowing certain kinds of properties to be ignored
|
|
20
20
|
- `"prefer"`: either `"class-property"` _(default)_ or `"parameter-property"`
|
|
21
21
|
|
|
22
|
-
### `
|
|
22
|
+
### `allow`
|
|
23
23
|
|
|
24
24
|
If you would like to ignore certain kinds of properties then you may pass an object containing `"allow"` as an array of any of the following options:
|
|
25
25
|
|
|
@@ -45,7 +45,7 @@ For example, to ignore `public` properties:
|
|
|
45
45
|
}
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
-
### `
|
|
48
|
+
### `prefer`
|
|
49
49
|
|
|
50
50
|
By default, the rule prefers class property (`"class-property"`).
|
|
51
51
|
You can switch it to instead preferring parameter property with (`"parameter-property"`).
|
|
@@ -61,7 +61,9 @@ enum Valid {
|
|
|
61
61
|
|
|
62
62
|
## Options
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
### `allowBitwiseExpressions`
|
|
65
|
+
|
|
66
|
+
When set to `true` will allow you to use bitwise expressions in enum initializer (default: `false`).
|
|
65
67
|
|
|
66
68
|
Examples of code for the `{ "allowBitwiseExpressions": true }` option:
|
|
67
69
|
|
|
@@ -167,6 +167,16 @@ foo ?? 'a string';
|
|
|
167
167
|
|
|
168
168
|
Also, if you would like to ignore all primitives types, you can set `ignorePrimitives: true`. It is equivalent to `ignorePrimitives: { string: true, number: true, bigint: true, boolean: true }`.
|
|
169
169
|
|
|
170
|
+
### `allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing`
|
|
171
|
+
|
|
172
|
+
If this is set to `false`, then the rule will error on every file whose `tsconfig.json` does _not_ have the `strictNullChecks` compiler option (or `strict`) set to `true`.
|
|
173
|
+
|
|
174
|
+
Without `strictNullChecks`, TypeScript essentially erases `undefined` and `null` from the types. This means when this rule inspects the types from a variable, **it will not be able to tell that the variable might be `null` or `undefined`**, which essentially makes this rule useless.
|
|
175
|
+
|
|
176
|
+
You should be using `strictNullChecks` to ensure complete type-safety in your codebase.
|
|
177
|
+
|
|
178
|
+
If for some reason you cannot turn on `strictNullChecks`, but still want to use this rule - you can use this option to allow it - but know that the behavior of this rule is _undefined_ with the compiler option turned off. We will not accept bug reports if you are using this option.
|
|
179
|
+
|
|
170
180
|
## When Not To Use It
|
|
171
181
|
|
|
172
182
|
If you are not using TypeScript 3.7 (or greater), then you will not be able to use this rule, as the operator is not supported.
|
|
@@ -57,7 +57,7 @@ interface Foo {
|
|
|
57
57
|
interface Foo {
|
|
58
58
|
new (arg: string[]): void;
|
|
59
59
|
}
|
|
60
|
-
const x = { foo(arg: string[]): void
|
|
60
|
+
const x = { foo(arg: string[]): void {} };
|
|
61
61
|
function foo(arg: string[]);
|
|
62
62
|
type Foo = (arg: string[]) => void;
|
|
63
63
|
interface Foo {
|
|
@@ -91,7 +91,7 @@ interface CustomFunction {
|
|
|
91
91
|
}
|
|
92
92
|
function custom2(arg: CustomFunction) {}
|
|
93
93
|
|
|
94
|
-
function union(arg: readonly string[] | ReadonlyArray<number
|
|
94
|
+
function union(arg: readonly string[] | ReadonlyArray<number>) {}
|
|
95
95
|
|
|
96
96
|
function primitive1(arg: string) {}
|
|
97
97
|
function primitive2(arg: number) {}
|
|
@@ -105,8 +105,11 @@ function primitive9(arg: string | number | undefined) {}
|
|
|
105
105
|
|
|
106
106
|
function fnSig(arg: () => void) {}
|
|
107
107
|
|
|
108
|
-
enum Foo {
|
|
109
|
-
|
|
108
|
+
enum Foo {
|
|
109
|
+
a,
|
|
110
|
+
b,
|
|
111
|
+
}
|
|
112
|
+
function enumArg(arg: Foo) {}
|
|
110
113
|
|
|
111
114
|
function symb1(arg: symbol) {}
|
|
112
115
|
const customSymbol = Symbol('a');
|
|
@@ -119,7 +122,7 @@ interface Foo {
|
|
|
119
122
|
interface Foo {
|
|
120
123
|
new (arg: readonly string[]): void;
|
|
121
124
|
}
|
|
122
|
-
const x = { foo(arg: readonly string[]): void
|
|
125
|
+
const x = { foo(arg: readonly string[]): void {} };
|
|
123
126
|
function foo(arg: readonly string[]);
|
|
124
127
|
type Foo = (arg: readonly string[]) => void;
|
|
125
128
|
interface Foo {
|
|
@@ -58,6 +58,74 @@ async function functionReturnsUnionWithPromiseImplicitly(p: boolean) {
|
|
|
58
58
|
}
|
|
59
59
|
```
|
|
60
60
|
|
|
61
|
+
## Options
|
|
62
|
+
|
|
63
|
+
### `allowAny`
|
|
64
|
+
|
|
65
|
+
Whether to ignore functions that return `any` and `unknown`.
|
|
66
|
+
If you want additional safety, consider turning this option off, as it makes the rule less able to catch incorrect Promise behaviors.
|
|
67
|
+
|
|
68
|
+
Examples of code with `{ "allowAny": false }`:
|
|
69
|
+
|
|
70
|
+
<!--tabs-->
|
|
71
|
+
|
|
72
|
+
#### ❌ Incorrect
|
|
73
|
+
|
|
74
|
+
```ts option='{ "allowAny": false }'
|
|
75
|
+
const returnsAny = () => ({}) as any;
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
#### ✅ Correct
|
|
79
|
+
|
|
80
|
+
```ts option='{ "allowAny": false }'
|
|
81
|
+
const returnsAny = async () => ({}) as any;
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### `allowedPromiseNames`
|
|
85
|
+
|
|
86
|
+
For projects that use constructs other than the global built-in `Promise` for asynchronous code.
|
|
87
|
+
This option allows specifying string names of classes or interfaces that cause a function to be checked as well.
|
|
88
|
+
|
|
89
|
+
Examples of code with `{ "allowedPromiseNames": ["Bluebird"] }`:
|
|
90
|
+
|
|
91
|
+
<!--tabs-->
|
|
92
|
+
|
|
93
|
+
#### ❌ Incorrect
|
|
94
|
+
|
|
95
|
+
```ts option='{ "allowedPromiseNames": ["Bluebird"] }'
|
|
96
|
+
import { Bluebird } from 'bluebird';
|
|
97
|
+
|
|
98
|
+
const returnsBluebird = () => new Bluebird(() => {});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### ✅ Correct
|
|
102
|
+
|
|
103
|
+
```ts option='{ "allowedPromiseNames": ["Bluebird"] }'
|
|
104
|
+
import { Bluebird } from 'bluebird';
|
|
105
|
+
|
|
106
|
+
const returnsBluebird = async () => new Bluebird(() => {});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### `checkArrowFunctions`
|
|
110
|
+
|
|
111
|
+
Whether to check arrow functions.
|
|
112
|
+
`true` by default, but can be set to `false` to ignore them.
|
|
113
|
+
|
|
114
|
+
### `checkFunctionDeclarations`
|
|
115
|
+
|
|
116
|
+
Whether to check standalone function declarations.
|
|
117
|
+
`true` by default, but can be set to `false` to ignore them.
|
|
118
|
+
|
|
119
|
+
### `checkFunctionExpressions`
|
|
120
|
+
|
|
121
|
+
Whether to check inline function expressions.
|
|
122
|
+
`true` by default, but can be set to `false` to ignore them.
|
|
123
|
+
|
|
124
|
+
### `checkMethodDeclarations`
|
|
125
|
+
|
|
126
|
+
Whether to check methods on classes and object literals
|
|
127
|
+
`true` by default, but can be set to `false` to ignore them.
|
|
128
|
+
|
|
61
129
|
## When Not To Use It
|
|
62
130
|
|
|
63
131
|
This rule can be difficult to enable on projects that use APIs which require functions to always be `async`.
|
|
@@ -82,6 +82,46 @@ type T4 =
|
|
|
82
82
|
|
|
83
83
|
## Options
|
|
84
84
|
|
|
85
|
+
### `checkIntersections`
|
|
86
|
+
|
|
87
|
+
Whether to check intersection types (`&`).
|
|
88
|
+
|
|
89
|
+
Examples of code with `{ "checkIntersections": true }` (the default):
|
|
90
|
+
|
|
91
|
+
<!--tabs-->
|
|
92
|
+
|
|
93
|
+
#### ❌ Incorrect
|
|
94
|
+
|
|
95
|
+
```ts option='{ "checkIntersections": true }'
|
|
96
|
+
type ExampleIntersection = B & A;
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### ✅ Correct
|
|
100
|
+
|
|
101
|
+
```ts option='{ "checkIntersections": true }'
|
|
102
|
+
type ExampleIntersection = A & B;
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### `checkUnions`
|
|
106
|
+
|
|
107
|
+
Whether to check union types (`|`).
|
|
108
|
+
|
|
109
|
+
Examples of code with `{ "checkUnions": true }` (the default):
|
|
110
|
+
|
|
111
|
+
<!--tabs-->
|
|
112
|
+
|
|
113
|
+
#### ❌ Incorrect
|
|
114
|
+
|
|
115
|
+
```ts option='{ "checkUnions": true }'
|
|
116
|
+
type ExampleUnion = B | A;
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### ✅ Correct
|
|
120
|
+
|
|
121
|
+
```ts option='{ "checkUnions": true }'
|
|
122
|
+
type ExampleUnion = A | B;
|
|
123
|
+
```
|
|
124
|
+
|
|
85
125
|
### `groupOrder`
|
|
86
126
|
|
|
87
127
|
Each constituent of the type is placed into a group, and then the rule sorts alphabetically within each group.
|
|
@@ -100,6 +140,22 @@ The ordering of groups is determined by this option.
|
|
|
100
140
|
- `union` - Union types (`A | B`)
|
|
101
141
|
- `nullish` - `null` and `undefined`
|
|
102
142
|
|
|
143
|
+
For example, configuring the rule with `{ "groupOrder": ["literal", "nullish" ]}`:
|
|
144
|
+
|
|
145
|
+
<!--tabs-->
|
|
146
|
+
|
|
147
|
+
#### ❌ Incorrect
|
|
148
|
+
|
|
149
|
+
```ts option='{ "groupOrder": ["literal", "nullish" ]}'
|
|
150
|
+
type ExampleGroup = null | 123;
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### ✅ Correct
|
|
154
|
+
|
|
155
|
+
```ts option='{ "groupOrder": ["literal", "nullish" ]}'
|
|
156
|
+
type ExampleGroup = 123 | null;
|
|
157
|
+
```
|
|
158
|
+
|
|
103
159
|
## When Not To Use It
|
|
104
160
|
|
|
105
161
|
This rule is purely a stylistic rule for maintaining consistency in your project.
|
|
@@ -6,12 +6,51 @@ description: 'Require switch-case statements to be exhaustive.'
|
|
|
6
6
|
>
|
|
7
7
|
> See **https://typescript-eslint.io/rules/switch-exhaustiveness-check** for documentation.
|
|
8
8
|
|
|
9
|
-
When working with union types or enums in TypeScript, it's common to want to write a `switch` statement intended to contain a `case` for each
|
|
9
|
+
When working with union types or enums in TypeScript, it's common to want to write a `switch` statement intended to contain a `case` for each possible type in the union or the enum.
|
|
10
10
|
However, if the union type or the enum changes, it's easy to forget to modify the cases to account for any new types.
|
|
11
11
|
|
|
12
12
|
This rule reports when a `switch` statement over a value typed as a union of literals or as an enum is missing a case for any of those literal types and does not have a `default` clause.
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
## Options
|
|
15
|
+
|
|
16
|
+
### `"allowDefaultCaseForExhaustiveSwitch"`
|
|
17
|
+
|
|
18
|
+
Defaults to true. If set to false, this rule will also report when a `switch` statement has a case for everything in a union and _also_ contains a `default` case. Thus, by setting this option to false, the rule becomes stricter.
|
|
19
|
+
|
|
20
|
+
When a `switch` statement over a union type is exhaustive, a final `default` case would be a form of dead code.
|
|
21
|
+
Additionally, if a new value is added to the union type, a `default` would prevent the `switch-exhaustiveness-check` rule from reporting on the new case not being handled in the `switch` statement.
|
|
22
|
+
|
|
23
|
+
#### `"allowDefaultCaseForExhaustiveSwitch"` Caveats
|
|
24
|
+
|
|
25
|
+
It can sometimes be useful to include a redundant `default` case on an exhaustive `switch` statement if it's possible for values to have types not represented by the union type.
|
|
26
|
+
For example, in applications that can have version mismatches between clients and servers, it's possible for a server running a newer software version to send a value not recognized by the client's older typings.
|
|
27
|
+
|
|
28
|
+
If your project has a small number of intentionally redundant `default` cases, you might want to use an [inline ESLint disable comment](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for each of them.
|
|
29
|
+
|
|
30
|
+
If your project has many intentionally redundant `default` cases, you may want to disable `allowDefaultCaseForExhaustiveSwitch` and use the [`default-case` core ESLint rule](https://eslint.org/docs/latest/rules/default-case) along with [a `satisfies never` check](https://www.typescriptlang.org/play?#code/C4TwDgpgBAYgTgVwJbCgXigcgIZjAGwkygB8sAjbAO2u0wG4AoRgMwSoGNgkB7KqBAGcI8ZMAAULRCgBcsacACUcwcDhIqAcygBvRlCiCA7ig4ALKJIWLd+g1A7ZhWXASJy99+3AjAEcfhw8QgApZA4iJi8AX2YvR2dMShoaTA87Lx8-AIpaGjCkCIYMqFiSgBMIFmwEfGB0rwMpMUNsbkEWJAhBKCoIADcIOCjGrP9A9gBrKh4jKgKikYNY5cZYoA).
|
|
31
|
+
|
|
32
|
+
### `requireDefaultForNonUnion`
|
|
33
|
+
|
|
34
|
+
Defaults to false. It set to true, this rule will also report when a `switch` statement switches over a non-union type (like a `number` or `string`, for example) and that `switch` statement does not have a `default` case. Thus, by setting this option to true, the rule becomes stricter.
|
|
35
|
+
|
|
36
|
+
This is generally desirable so that `number` and `string` switches will be subject to the same exhaustive checks that your other switches are.
|
|
37
|
+
|
|
38
|
+
Examples of additional **incorrect** code for this rule with `{ requireDefaultForNonUnion: true }`:
|
|
39
|
+
|
|
40
|
+
```ts option='{ "requireDefaultForNonUnion": true }' showPlaygroundButton
|
|
41
|
+
const value: number = Math.floor(Math.random() * 3);
|
|
42
|
+
|
|
43
|
+
switch (value) {
|
|
44
|
+
case 0:
|
|
45
|
+
return 0;
|
|
46
|
+
case 1:
|
|
47
|
+
return 1;
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Since `value` is a non-union type it requires the switch case to have a default clause only with `requireDefaultForNonUnion` enabled.
|
|
52
|
+
|
|
53
|
+
<!--/tabs-->
|
|
15
54
|
|
|
16
55
|
## Examples
|
|
17
56
|
|
|
@@ -181,27 +220,6 @@ switch (fruit) {
|
|
|
181
220
|
|
|
182
221
|
<!--/tabs-->
|
|
183
222
|
|
|
184
|
-
## Options
|
|
185
|
-
|
|
186
|
-
### `requireDefaultForNonUnion`
|
|
187
|
-
|
|
188
|
-
Examples of additional **incorrect** code for this rule with `{ requireDefaultForNonUnion: true }`:
|
|
189
|
-
|
|
190
|
-
```ts option='{ "requireDefaultForNonUnion": true }' showPlaygroundButton
|
|
191
|
-
const value: number = Math.floor(Math.random() * 3);
|
|
192
|
-
|
|
193
|
-
switch (value) {
|
|
194
|
-
case 0:
|
|
195
|
-
return 0;
|
|
196
|
-
case 1:
|
|
197
|
-
return 1;
|
|
198
|
-
}
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
Since `value` is a non-union type it requires the switch case to have a default clause only with `requireDefaultForNonUnion` enabled.
|
|
202
|
-
|
|
203
|
-
<!--/tabs-->
|
|
204
|
-
|
|
205
223
|
## When Not To Use It
|
|
206
224
|
|
|
207
225
|
If you don't frequently `switch` over union types or enums with many parts, or intentionally wish to leave out some parts, this rule may not be for you.
|
|
@@ -8,46 +8,94 @@ description: 'Disallow certain triple slash directives in favor of ES6-style imp
|
|
|
8
8
|
|
|
9
9
|
TypeScript's `///` triple-slash references are a way to indicate that types from another module are available in a file.
|
|
10
10
|
Use of triple-slash reference type directives is generally discouraged in favor of ECMAScript Module `import`s.
|
|
11
|
-
This rule reports on the use of `/// <reference
|
|
11
|
+
This rule reports on the use of `/// <reference lib="..." />`, `/// <reference path="..." />`, or `/// <reference types="..." />` directives.
|
|
12
12
|
|
|
13
13
|
## Options
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
Any number of the three kinds of references can be specified as an option.
|
|
16
|
+
Specifying `'always'` disables this lint rule for that kind of reference.
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
### `lib`
|
|
19
|
+
|
|
20
|
+
When set to `'never'`, bans `/// <reference lib="..." />` and enforces using an `import` instead:
|
|
21
|
+
|
|
22
|
+
<!--tabs-->
|
|
23
|
+
|
|
24
|
+
#### ❌ Incorrect
|
|
25
|
+
|
|
26
|
+
```ts option='{ "lib": "never" }'
|
|
27
|
+
/// <reference lib="code" />
|
|
28
|
+
|
|
29
|
+
globalThis.value;
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
#### ✅ Correct
|
|
33
|
+
|
|
34
|
+
```ts option='{ "lib": "never" }'
|
|
35
|
+
import { value } from 'code';
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### `path`
|
|
39
|
+
|
|
40
|
+
When set to `'never'`, bans `/// <reference path="..." />` and enforces using an `import` instead:
|
|
41
|
+
|
|
42
|
+
<!--tabs-->
|
|
43
|
+
|
|
44
|
+
#### ❌ Incorrect
|
|
45
|
+
|
|
46
|
+
```ts option='{ "path": "never" }'
|
|
47
|
+
/// <reference path="code" />
|
|
48
|
+
|
|
49
|
+
globalThis.value;
|
|
21
50
|
```
|
|
22
51
|
|
|
23
|
-
|
|
52
|
+
#### ✅ Correct
|
|
24
53
|
|
|
25
|
-
```ts option='{ "
|
|
26
|
-
|
|
27
|
-
import * as foo from 'foo';
|
|
54
|
+
```ts option='{ "path": "never" }'
|
|
55
|
+
import { value } from 'code';
|
|
28
56
|
```
|
|
29
57
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
58
|
+
### `types`
|
|
59
|
+
|
|
60
|
+
When set to `'never'`, bans `/// <reference types="..." />` and enforces using an `import` instead:
|
|
61
|
+
|
|
62
|
+
<!--tabs-->
|
|
63
|
+
|
|
64
|
+
#### ❌ Incorrect
|
|
65
|
+
|
|
66
|
+
```ts option='{ "types": "never" }'
|
|
67
|
+
/// <reference types="code" />
|
|
68
|
+
|
|
69
|
+
globalThis.value;
|
|
33
70
|
```
|
|
34
71
|
|
|
35
|
-
|
|
72
|
+
#### ✅ Correct
|
|
36
73
|
|
|
37
|
-
```ts option='{ "
|
|
38
|
-
|
|
39
|
-
/// <reference types="bar" />
|
|
40
|
-
/// <reference lib="baz" />
|
|
74
|
+
```ts option='{ "types": "never" }'
|
|
75
|
+
import { value } from 'code';
|
|
41
76
|
```
|
|
42
77
|
|
|
43
|
-
|
|
78
|
+
<!-- /tabs -->
|
|
44
79
|
|
|
45
|
-
|
|
46
|
-
|
|
80
|
+
The `types` option may alternately be given a `"prefer-import"` value.
|
|
81
|
+
Doing so indicates the rule should only report if there is already an `import` from the same location:
|
|
82
|
+
|
|
83
|
+
<!--tabs-->
|
|
84
|
+
|
|
85
|
+
#### ❌ Incorrect
|
|
86
|
+
|
|
87
|
+
```ts option='{ "types": "prefer-import" }'
|
|
88
|
+
/// <reference types="code" />
|
|
89
|
+
|
|
90
|
+
import { valueA } from 'code';
|
|
91
|
+
|
|
92
|
+
globalThis.valueB;
|
|
47
93
|
```
|
|
48
94
|
|
|
49
|
-
|
|
50
|
-
|
|
95
|
+
#### ✅ Correct
|
|
96
|
+
|
|
97
|
+
```ts option='{ "types": "prefer-import" }'
|
|
98
|
+
import { valueA, valueB } from 'code';
|
|
51
99
|
```
|
|
52
100
|
|
|
53
101
|
## When Not To Use It
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typescript-eslint/eslint-plugin",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.17.0",
|
|
4
4
|
"description": "TypeScript plugin for ESLint",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -57,10 +57,10 @@
|
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
59
|
"@eslint-community/regexpp": "^4.5.1",
|
|
60
|
-
"@typescript-eslint/scope-manager": "6.
|
|
61
|
-
"@typescript-eslint/type-utils": "6.
|
|
62
|
-
"@typescript-eslint/utils": "6.
|
|
63
|
-
"@typescript-eslint/visitor-keys": "6.
|
|
60
|
+
"@typescript-eslint/scope-manager": "6.17.0",
|
|
61
|
+
"@typescript-eslint/type-utils": "6.17.0",
|
|
62
|
+
"@typescript-eslint/utils": "6.17.0",
|
|
63
|
+
"@typescript-eslint/visitor-keys": "6.17.0",
|
|
64
64
|
"debug": "^4.3.4",
|
|
65
65
|
"graphemer": "^1.4.0",
|
|
66
66
|
"ignore": "^5.2.4",
|
|
@@ -73,8 +73,8 @@
|
|
|
73
73
|
"@types/debug": "*",
|
|
74
74
|
"@types/marked": "*",
|
|
75
75
|
"@types/natural-compare": "*",
|
|
76
|
-
"@typescript-eslint/rule-schema-to-typescript-types": "6.
|
|
77
|
-
"@typescript-eslint/rule-tester": "6.
|
|
76
|
+
"@typescript-eslint/rule-schema-to-typescript-types": "6.17.0",
|
|
77
|
+
"@typescript-eslint/rule-tester": "6.17.0",
|
|
78
78
|
"ajv": "^6.12.6",
|
|
79
79
|
"chalk": "^5.3.0",
|
|
80
80
|
"cross-fetch": "*",
|
|
@@ -103,5 +103,5 @@
|
|
|
103
103
|
"type": "opencollective",
|
|
104
104
|
"url": "https://opencollective.com/typescript-eslint"
|
|
105
105
|
},
|
|
106
|
-
"gitHead": "
|
|
106
|
+
"gitHead": "e566a5dda347470b8ced3cc301b7e4d3e7ed721b"
|
|
107
107
|
}
|