@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,184 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow certain types in boolean expressions.'
|
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/strict-boolean-expressions** for documentation.
|
11
|
-
|
12
|
-
Forbids usage of non-boolean types in expressions where a boolean is expected.
|
13
|
-
`boolean` and `never` types are always allowed.
|
14
|
-
Additional types which are considered safe in a boolean context can be configured via options.
|
15
|
-
|
16
|
-
The following nodes are considered boolean expressions and their type is checked:
|
17
|
-
|
18
|
-
- Argument to the logical negation operator (`!arg`).
|
19
|
-
- The condition in a conditional expression (`cond ? x : y`).
|
20
|
-
- Conditions for `if`, `for`, `while`, and `do-while` statements.
|
21
|
-
- Operands of logical binary operators (`lhs || rhs` and `lhs && rhs`).
|
22
|
-
- Right-hand side operand is ignored when it's not a descendant of another boolean expression.
|
23
|
-
This is to allow usage of boolean operators for their short-circuiting behavior.
|
24
|
-
- Asserted argument of an assertion function (`assert(arg)`).
|
25
|
-
- Return type of array predicate functions such as `filter()`, `some()`, etc.
|
26
|
-
|
27
|
-
## Examples
|
28
|
-
|
29
|
-
<Tabs>
|
30
|
-
<TabItem value="❌ Incorrect">
|
31
|
-
|
32
|
-
```ts
|
33
|
-
// nullable numbers are considered unsafe by default
|
34
|
-
declare const num: number | undefined;
|
35
|
-
if (num) {
|
36
|
-
console.log('num is defined');
|
37
|
-
}
|
38
|
-
|
39
|
-
// nullable strings are considered unsafe by default
|
40
|
-
declare const str: string | null;
|
41
|
-
if (!str) {
|
42
|
-
console.log('str is empty');
|
43
|
-
}
|
44
|
-
|
45
|
-
// nullable booleans are considered unsafe by default
|
46
|
-
function foo(bool?: boolean) {
|
47
|
-
if (bool) {
|
48
|
-
bar();
|
49
|
-
}
|
50
|
-
}
|
51
|
-
|
52
|
-
// `any`, unconstrained generics and unions of more than one primitive type are disallowed
|
53
|
-
const foo = <T>(arg: T) => (arg ? 1 : 0);
|
54
|
-
|
55
|
-
// always-truthy and always-falsy types are disallowed
|
56
|
-
let obj = {};
|
57
|
-
while (obj) {
|
58
|
-
obj = getObj();
|
59
|
-
}
|
60
|
-
|
61
|
-
// assertion functions without an `is` are boolean contexts.
|
62
|
-
declare function assert(value: unknown): asserts value;
|
63
|
-
let maybeString = Math.random() > 0.5 ? '' : undefined;
|
64
|
-
assert(maybeString);
|
65
|
-
|
66
|
-
// array predicates' return types are boolean contexts.
|
67
|
-
['one', null].filter(x => x);
|
68
|
-
```
|
69
|
-
|
70
|
-
</TabItem>
|
71
|
-
<TabItem value="✅ Correct">
|
72
|
-
|
73
|
-
```tsx
|
74
|
-
// nullable values should be checked explicitly against null or undefined
|
75
|
-
let num: number | undefined = 0;
|
76
|
-
if (num != null) {
|
77
|
-
console.log('num is defined');
|
78
|
-
}
|
79
|
-
|
80
|
-
let str: string | null = null;
|
81
|
-
if (str != null && !str) {
|
82
|
-
console.log('str is empty');
|
83
|
-
}
|
84
|
-
|
85
|
-
function foo(bool?: boolean) {
|
86
|
-
if (bool ?? false) {
|
87
|
-
bar();
|
88
|
-
}
|
89
|
-
}
|
90
|
-
|
91
|
-
// `any` types should be converted to boolean explicitly
|
92
|
-
const foo = (arg: any) => (Boolean(arg) ? 1 : 0);
|
93
|
-
```
|
94
|
-
|
95
|
-
</TabItem>
|
96
|
-
</Tabs>
|
97
|
-
|
98
|
-
## Options
|
99
|
-
|
100
|
-
### `allowString`
|
101
|
-
|
102
|
-
{/* insert option description */}
|
103
|
-
|
104
|
-
This can be safe because strings have only one falsy value (`""`).
|
105
|
-
Set this to `false` if you prefer the explicit `str != ""` or `str.length > 0` style.
|
106
|
-
|
107
|
-
### `allowNumber`
|
108
|
-
|
109
|
-
{/* insert option description */}
|
110
|
-
|
111
|
-
This can be safe because numbers have only two falsy values (`0` and `NaN`).
|
112
|
-
Set this to `false` if you prefer the explicit `num != 0` and `!Number.isNaN(num)` style.
|
113
|
-
|
114
|
-
### `allowNullableObject`
|
115
|
-
|
116
|
-
{/* insert option description */}
|
117
|
-
|
118
|
-
This can be safe because objects, functions, and symbols don't have falsy values.
|
119
|
-
Set this to `false` if you prefer the explicit `obj != null` style.
|
120
|
-
|
121
|
-
### `allowNullableBoolean`
|
122
|
-
|
123
|
-
{/* insert option description */}
|
124
|
-
|
125
|
-
This is unsafe because nullable booleans can be either `false` or nullish.
|
126
|
-
Set this to `false` if you want to enforce explicit `bool ?? false` or `bool ?? true` style.
|
127
|
-
Set this to `true` if you don't mind implicitly treating false the same as a nullish value.
|
128
|
-
|
129
|
-
### `allowNullableString`
|
130
|
-
|
131
|
-
{/* insert option description */}
|
132
|
-
|
133
|
-
This is unsafe because nullable strings can be either an empty string or nullish.
|
134
|
-
Set this to `true` if you don't mind implicitly treating an empty string the same as a nullish value.
|
135
|
-
|
136
|
-
### `allowNullableNumber`
|
137
|
-
|
138
|
-
{/* insert option description */}
|
139
|
-
|
140
|
-
This is unsafe because nullable numbers can be either a falsy number or nullish.
|
141
|
-
Set this to `true` if you don't mind implicitly treating zero or NaN the same as a nullish value.
|
142
|
-
|
143
|
-
### `allowNullableEnum`
|
144
|
-
|
145
|
-
{/* insert option description */}
|
146
|
-
|
147
|
-
This is unsafe because nullable enums can be either a falsy number or nullish.
|
148
|
-
Set this to `true` if you don't mind implicitly treating an enum whose value is zero the same as a nullish value.
|
149
|
-
|
150
|
-
### `allowAny`
|
151
|
-
|
152
|
-
{/* insert option description */}
|
153
|
-
|
154
|
-
This is unsafe for because `any` allows any values and disables many type checking checks.
|
155
|
-
Set this to `true` at your own risk.
|
156
|
-
|
157
|
-
### `allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing`
|
158
|
-
|
159
|
-
{/* insert option description */}
|
160
|
-
|
161
|
-
:::danger Deprecated
|
162
|
-
|
163
|
-
This option will be removed in the next major version of typescript-eslint.
|
164
|
-
|
165
|
-
:::
|
166
|
-
|
167
|
-
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`.
|
168
|
-
|
169
|
-
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 a lot less useful.
|
170
|
-
|
171
|
-
You should be using `strictNullChecks` to ensure complete type-safety in your codebase.
|
172
|
-
|
173
|
-
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.
|
174
|
-
|
175
|
-
## When Not To Use It
|
176
|
-
|
177
|
-
If your project isn't likely to experience bugs from falsy non-boolean values being used in logical conditions, you can skip enabling this rule.
|
178
|
-
|
179
|
-
Otherwise, this rule can be quite strict around requiring exact comparisons in logical checks.
|
180
|
-
If you prefer more succinct checks over more precise boolean logic, this rule might not be for you.
|
181
|
-
|
182
|
-
## Related To
|
183
|
-
|
184
|
-
- [no-unnecessary-condition](./no-unnecessary-condition.mdx) - Similar rule which reports always-truthy and always-falsy values in conditions
|
@@ -1,280 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Require switch-case statements to be exhaustive.'
|
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/switch-exhaustiveness-check** for documentation.
|
11
|
-
|
12
|
-
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.
|
13
|
-
However, if the union type or the enum changes, it's easy to forget to modify the cases to account for any new types.
|
14
|
-
|
15
|
-
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.
|
16
|
-
|
17
|
-
## Options
|
18
|
-
|
19
|
-
### `allowDefaultCaseForExhaustiveSwitch`
|
20
|
-
|
21
|
-
{/* insert option description */}
|
22
|
-
|
23
|
-
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.
|
24
|
-
|
25
|
-
When a `switch` statement over a union type is exhaustive, a final `default` case would be a form of dead code.
|
26
|
-
Additionally, if a new value is added to the union type and you're using [`considerDefaultExhaustiveForUnions`](#considerDefaultExhaustiveForUnions), a `default` would prevent the `switch-exhaustiveness-check` rule from reporting on the new case not being handled in the `switch` statement.
|
27
|
-
|
28
|
-
#### `allowDefaultCaseForExhaustiveSwitch` Caveats
|
29
|
-
|
30
|
-
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.
|
31
|
-
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.
|
32
|
-
|
33
|
-
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.
|
34
|
-
|
35
|
-
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).
|
36
|
-
|
37
|
-
### `requireDefaultForNonUnion`
|
38
|
-
|
39
|
-
{/* insert option description */}
|
40
|
-
|
41
|
-
If 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.
|
42
|
-
|
43
|
-
This is generally desirable so that `number` and `string` switches will be subject to the same exhaustive checks that your other switches are.
|
44
|
-
|
45
|
-
Examples of additional **incorrect** code for this rule with `{ requireDefaultForNonUnion: true }`:
|
46
|
-
|
47
|
-
```ts option='{ "requireDefaultForNonUnion": true }' showPlaygroundButton
|
48
|
-
const value: number = Math.floor(Math.random() * 3);
|
49
|
-
|
50
|
-
switch (value) {
|
51
|
-
case 0:
|
52
|
-
return 0;
|
53
|
-
case 1:
|
54
|
-
return 1;
|
55
|
-
}
|
56
|
-
```
|
57
|
-
|
58
|
-
Since `value` is a non-union type it requires the switch case to have a default clause only with `requireDefaultForNonUnion` enabled.
|
59
|
-
|
60
|
-
### `considerDefaultExhaustiveForUnions`
|
61
|
-
|
62
|
-
{/* insert option description */}
|
63
|
-
|
64
|
-
If set to true, a `switch` statement over a union type that includes a `default` case is considered exhaustive.
|
65
|
-
Otherwise, the rule enforces explicitly handling every constituent of the union type with their own explicit `case`.
|
66
|
-
Keeping this option disabled can be useful if you want to make sure every value added to the union receives explicit handling, with the `default` case reserved for reporting an error.
|
67
|
-
|
68
|
-
Examples of additional **correct** code with `{ considerDefaultExhaustiveForUnions: true }`:
|
69
|
-
|
70
|
-
```ts option='{ "considerDefaultExhaustiveForUnions": true }' showPlaygroundButton
|
71
|
-
declare const literal: 'a' | 'b';
|
72
|
-
|
73
|
-
switch (literal) {
|
74
|
-
case 'a':
|
75
|
-
break;
|
76
|
-
default:
|
77
|
-
break;
|
78
|
-
}
|
79
|
-
```
|
80
|
-
|
81
|
-
### `defaultCaseCommentPattern`
|
82
|
-
|
83
|
-
{/* insert option description */}
|
84
|
-
|
85
|
-
Default: `/^no default$/iu`.
|
86
|
-
|
87
|
-
It can sometimes be preferable to omit the default case for only some switch statements.
|
88
|
-
For those situations, this rule can be given a pattern for a comment that's allowed to take the place of a `default:`.
|
89
|
-
|
90
|
-
Examples of additional **correct** code with `{ defaultCaseCommentPattern: "^skip\\sdefault" }`:
|
91
|
-
|
92
|
-
```ts option='{ "defaultCaseCommentPattern": "^skip default" }' showPlaygroundButton
|
93
|
-
declare const value: 'a' | 'b';
|
94
|
-
|
95
|
-
switch (value) {
|
96
|
-
case 'a':
|
97
|
-
break;
|
98
|
-
// skip default
|
99
|
-
}
|
100
|
-
```
|
101
|
-
|
102
|
-
## Examples
|
103
|
-
|
104
|
-
When the switch doesn't have exhaustive cases, either filling them all out or adding a default (if you have `considerDefaultExhaustiveForUnions` enabled) will address the rule's complaint.
|
105
|
-
|
106
|
-
Here are some examples of code working with a union of literals:
|
107
|
-
|
108
|
-
<Tabs>
|
109
|
-
<TabItem value="❌ Incorrect">
|
110
|
-
|
111
|
-
```ts
|
112
|
-
type Day =
|
113
|
-
| 'Monday'
|
114
|
-
| 'Tuesday'
|
115
|
-
| 'Wednesday'
|
116
|
-
| 'Thursday'
|
117
|
-
| 'Friday'
|
118
|
-
| 'Saturday'
|
119
|
-
| 'Sunday';
|
120
|
-
|
121
|
-
declare const day: Day;
|
122
|
-
let result = 0;
|
123
|
-
|
124
|
-
switch (day) {
|
125
|
-
case 'Monday':
|
126
|
-
result = 1;
|
127
|
-
break;
|
128
|
-
}
|
129
|
-
```
|
130
|
-
|
131
|
-
</TabItem>
|
132
|
-
<TabItem value="✅ Correct (Exhaustive)">
|
133
|
-
|
134
|
-
```ts
|
135
|
-
type Day =
|
136
|
-
| 'Monday'
|
137
|
-
| 'Tuesday'
|
138
|
-
| 'Wednesday'
|
139
|
-
| 'Thursday'
|
140
|
-
| 'Friday'
|
141
|
-
| 'Saturday'
|
142
|
-
| 'Sunday';
|
143
|
-
|
144
|
-
declare const day: Day;
|
145
|
-
let result = 0;
|
146
|
-
|
147
|
-
switch (day) {
|
148
|
-
case 'Monday':
|
149
|
-
result = 1;
|
150
|
-
break;
|
151
|
-
case 'Tuesday':
|
152
|
-
result = 2;
|
153
|
-
break;
|
154
|
-
case 'Wednesday':
|
155
|
-
result = 3;
|
156
|
-
break;
|
157
|
-
case 'Thursday':
|
158
|
-
result = 4;
|
159
|
-
break;
|
160
|
-
case 'Friday':
|
161
|
-
result = 5;
|
162
|
-
break;
|
163
|
-
case 'Saturday':
|
164
|
-
result = 6;
|
165
|
-
break;
|
166
|
-
case 'Sunday':
|
167
|
-
result = 7;
|
168
|
-
break;
|
169
|
-
}
|
170
|
-
```
|
171
|
-
|
172
|
-
</TabItem>
|
173
|
-
<TabItem value="✅ Correct (Defaulted)">
|
174
|
-
|
175
|
-
```ts option='{ "considerDefaultExhaustiveForUnions": true }'
|
176
|
-
// requires `considerDefaultExhaustiveForUnions` to be set to true
|
177
|
-
|
178
|
-
type Day =
|
179
|
-
| 'Monday'
|
180
|
-
| 'Tuesday'
|
181
|
-
| 'Wednesday'
|
182
|
-
| 'Thursday'
|
183
|
-
| 'Friday'
|
184
|
-
| 'Saturday'
|
185
|
-
| 'Sunday';
|
186
|
-
|
187
|
-
declare const day: Day;
|
188
|
-
let result = 0;
|
189
|
-
|
190
|
-
switch (day) {
|
191
|
-
case 'Monday':
|
192
|
-
result = 1;
|
193
|
-
break;
|
194
|
-
default:
|
195
|
-
result = 42;
|
196
|
-
}
|
197
|
-
```
|
198
|
-
|
199
|
-
</TabItem>
|
200
|
-
</Tabs>
|
201
|
-
|
202
|
-
Likewise, here are some examples of code working with an enum:
|
203
|
-
|
204
|
-
<Tabs>
|
205
|
-
<TabItem value="❌ Incorrect">
|
206
|
-
|
207
|
-
```ts
|
208
|
-
enum Fruit {
|
209
|
-
Apple,
|
210
|
-
Banana,
|
211
|
-
Cherry,
|
212
|
-
}
|
213
|
-
|
214
|
-
declare const fruit: Fruit;
|
215
|
-
|
216
|
-
switch (fruit) {
|
217
|
-
case Fruit.Apple:
|
218
|
-
console.log('an apple');
|
219
|
-
break;
|
220
|
-
}
|
221
|
-
```
|
222
|
-
|
223
|
-
</TabItem>
|
224
|
-
<TabItem value="✅ Correct (Exhaustive)">
|
225
|
-
|
226
|
-
```ts
|
227
|
-
enum Fruit {
|
228
|
-
Apple,
|
229
|
-
Banana,
|
230
|
-
Cherry,
|
231
|
-
}
|
232
|
-
|
233
|
-
declare const fruit: Fruit;
|
234
|
-
|
235
|
-
switch (fruit) {
|
236
|
-
case Fruit.Apple:
|
237
|
-
console.log('an apple');
|
238
|
-
break;
|
239
|
-
|
240
|
-
case Fruit.Banana:
|
241
|
-
console.log('a banana');
|
242
|
-
break;
|
243
|
-
|
244
|
-
case Fruit.Cherry:
|
245
|
-
console.log('a cherry');
|
246
|
-
break;
|
247
|
-
}
|
248
|
-
```
|
249
|
-
|
250
|
-
</TabItem>
|
251
|
-
<TabItem value="✅ Correct (Defaulted)">
|
252
|
-
|
253
|
-
```ts option='{ "considerDefaultExhaustiveForUnions": true }'
|
254
|
-
// requires `considerDefaultExhaustiveForUnions` to be set to true
|
255
|
-
|
256
|
-
enum Fruit {
|
257
|
-
Apple,
|
258
|
-
Banana,
|
259
|
-
Cherry,
|
260
|
-
}
|
261
|
-
|
262
|
-
declare const fruit: Fruit;
|
263
|
-
|
264
|
-
switch (fruit) {
|
265
|
-
case Fruit.Apple:
|
266
|
-
console.log('an apple');
|
267
|
-
break;
|
268
|
-
|
269
|
-
default:
|
270
|
-
console.log('a fruit');
|
271
|
-
break;
|
272
|
-
}
|
273
|
-
```
|
274
|
-
|
275
|
-
</TabItem>
|
276
|
-
</Tabs>
|
277
|
-
|
278
|
-
## When Not To Use It
|
279
|
-
|
280
|
-
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.
|
@@ -1,129 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow certain triple slash directives in favor of ES6-style import declarations.'
|
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/triple-slash-reference** for documentation.
|
11
|
-
|
12
|
-
TypeScript's `///` triple-slash references are a way to indicate that types from another module are available in a file.
|
13
|
-
Use of triple-slash reference type directives is generally discouraged in favor of ECMAScript Module `import`s.
|
14
|
-
This rule reports on the use of `/// <reference lib="..." />`, `/// <reference path="..." />`, or `/// <reference types="..." />` directives.
|
15
|
-
|
16
|
-
## Options
|
17
|
-
|
18
|
-
Any number of the three kinds of references can be specified as an option.
|
19
|
-
Specifying `'always'` disables this lint rule for that kind of reference.
|
20
|
-
|
21
|
-
### `lib`
|
22
|
-
|
23
|
-
{/* insert option description */}
|
24
|
-
|
25
|
-
When set to `'never'`, bans `/// <reference lib="..." />` and enforces using an `import` instead:
|
26
|
-
|
27
|
-
<Tabs>
|
28
|
-
<TabItem value="❌ Incorrect">
|
29
|
-
|
30
|
-
```ts option='{ "lib": "never" }'
|
31
|
-
/// <reference lib="code" />
|
32
|
-
|
33
|
-
globalThis.value;
|
34
|
-
```
|
35
|
-
|
36
|
-
</TabItem>
|
37
|
-
<TabItem value="✅ Correct">
|
38
|
-
|
39
|
-
```ts option='{ "lib": "never" }'
|
40
|
-
import { value } from 'code';
|
41
|
-
```
|
42
|
-
|
43
|
-
</TabItem>
|
44
|
-
</Tabs>
|
45
|
-
|
46
|
-
### `path`
|
47
|
-
|
48
|
-
{/* insert option description */}
|
49
|
-
|
50
|
-
When set to `'never'`, bans `/// <reference path="..." />` and enforces using an `import` instead:
|
51
|
-
|
52
|
-
<Tabs>
|
53
|
-
<TabItem value="❌ Incorrect">
|
54
|
-
|
55
|
-
```ts option='{ "path": "never" }'
|
56
|
-
/// <reference path="code" />
|
57
|
-
|
58
|
-
globalThis.value;
|
59
|
-
```
|
60
|
-
|
61
|
-
</TabItem>
|
62
|
-
<TabItem value="✅ Correct">
|
63
|
-
|
64
|
-
```ts option='{ "path": "never" }'
|
65
|
-
import { value } from 'code';
|
66
|
-
```
|
67
|
-
|
68
|
-
</TabItem>
|
69
|
-
</Tabs>
|
70
|
-
|
71
|
-
### `types`
|
72
|
-
|
73
|
-
{/* insert option description */}
|
74
|
-
|
75
|
-
When set to `'never'`, bans `/// <reference types="..." />` and enforces using an `import` instead:
|
76
|
-
|
77
|
-
<Tabs>
|
78
|
-
<TabItem value="❌ Incorrect">
|
79
|
-
|
80
|
-
```ts option='{ "types": "never" }'
|
81
|
-
/// <reference types="code" />
|
82
|
-
|
83
|
-
globalThis.value;
|
84
|
-
```
|
85
|
-
|
86
|
-
</TabItem>
|
87
|
-
<TabItem value="✅ Correct">
|
88
|
-
|
89
|
-
```ts option='{ "types": "never" }'
|
90
|
-
import { value } from 'code';
|
91
|
-
```
|
92
|
-
|
93
|
-
</TabItem>
|
94
|
-
</Tabs>
|
95
|
-
|
96
|
-
The `types` option may alternately be given a `"prefer-import"` value.
|
97
|
-
Doing so indicates the rule should only report if there is already an `import` from the same location:
|
98
|
-
|
99
|
-
<Tabs>
|
100
|
-
<TabItem value="❌ Incorrect">
|
101
|
-
|
102
|
-
```ts option='{ "types": "prefer-import" }'
|
103
|
-
/// <reference types="code" />
|
104
|
-
|
105
|
-
import { valueA } from 'code';
|
106
|
-
|
107
|
-
globalThis.valueB;
|
108
|
-
```
|
109
|
-
|
110
|
-
</TabItem>
|
111
|
-
<TabItem value="✅ Correct">
|
112
|
-
|
113
|
-
```ts option='{ "types": "prefer-import" }'
|
114
|
-
import { valueA, valueB } from 'code';
|
115
|
-
```
|
116
|
-
|
117
|
-
</TabItem>
|
118
|
-
</Tabs>
|
119
|
-
|
120
|
-
## When Not To Use It
|
121
|
-
|
122
|
-
Most modern TypeScript projects generally use `import` statements to bring in types.
|
123
|
-
It's rare to need a `///` triple-slash reference outside of auto-generated code.
|
124
|
-
If your project is a rare one with one of those use cases, this rule might not be for you.
|
125
|
-
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.
|
126
|
-
|
127
|
-
## When Not To Use It
|
128
|
-
|
129
|
-
If you want to use all flavors of triple slash reference directives.
|
@@ -1,15 +0,0 @@
|
|
1
|
-
---
|
2
|
-
displayed_sidebar: rulesSidebar
|
3
|
-
---
|
4
|
-
|
5
|
-
:::danger Deprecated
|
6
|
-
|
7
|
-
This rule has been moved to the [ESLint stylistic plugin](https://eslint.style).
|
8
|
-
See [#8072](https://github.com/typescript-eslint/typescript-eslint/issues/8072) and [#8074](https://github.com/typescript-eslint/typescript-eslint/issues/8074) for more information.
|
9
|
-
|
10
|
-
:::
|
11
|
-
|
12
|
-
<!-- This doc file has been left on purpose to help direct people to the stylistic plugin.
|
13
|
-
|
14
|
-
Note that there is no actual way to get to this page in the normal navigation,
|
15
|
-
so end-users will only be able to get to this page from the search bar. -->
|