Skip to content

fix(eslint-plugin): [switch-exhaustiveness-check] invert considerDefaultExhaustiveForUnions #10223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 8 additions & 34 deletions packages/eslint-plugin/docs/rules/switch-exhaustiveness-check.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ If set to true, a `switch` statement over a union type that includes a `default`
Otherwise, the rule enforces explicitly handling every constituent of the union type with their own explicit `case`.
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.

Examples of code with `{ considerDefaultExhaustiveForUnions: true }`:

<Tabs>
<TabItem value="❌ Incorrect">
Examples of additional **correct** code with `{ considerDefaultExhaustiveForUnions: true }`:

```ts option='{ "considerDefaultExhaustiveForUnions": true }' showPlaygroundButton
declare const literal: 'a' | 'b';
Expand All @@ -81,36 +78,9 @@ switch (literal) {
}
```

</TabItem>

<TabItem value="✅ Correct">

```ts option='{ "considerDefaultExhaustiveForUnions": true }' showPlaygroundButton
declare const literal: 'a' | 'b';

switch (literal) {
case 'a':
break;
case 'b':
break;
default:
break;
}

switch (literal) {
case 'a':
break;
case 'b':
break;
}
```

</TabItem>
</Tabs>

## Examples

When the switch doesn't have exhaustive cases, either filling them all out or adding a default will correct the rule's complaint.
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.

Here are some examples of code working with a union of literals:

Expand Down Expand Up @@ -181,7 +151,9 @@ switch (day) {
</TabItem>
<TabItem value="✅ Correct (Defaulted)">

```ts
```ts option='{ "considerDefaultExhaustiveForUnions": true }'
// requires `considerDefaultExhaustiveForUnions` to be set to true

type Day =
| 'Monday'
| 'Tuesday'
Expand Down Expand Up @@ -257,7 +229,9 @@ switch (fruit) {
</TabItem>
<TabItem value="✅ Correct (Defaulted)">

```ts
```ts option='{ "considerDefaultExhaustiveForUnions": true }'
// requires `considerDefaultExhaustiveForUnions` to be set to true

enum Fruit {
Apple,
Banana,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ export default createRule<Options, MessageIds>({
const { defaultCase, missingLiteralBranchTypes, symbolName } =
switchMetadata;

// Unless considerDefaultExhaustiveForUnions is enabled, the presence of a default case
// If considerDefaultExhaustiveForUnions is enabled, the presence of a default case
// always makes the switch exhaustive.
if (!considerDefaultExhaustiveForUnions && defaultCase != null) {
if (considerDefaultExhaustiveForUnions && defaultCase != null) {
return;
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ function test(value: Union): number {
}
`,
// Switch contains default clause.
`
{
code: `
type Day =
| 'Monday'
| 'Tuesday'
Expand All @@ -166,7 +167,13 @@ switch (day) {
result = 42;
}
}
`,
`,
options: [
{
considerDefaultExhaustiveForUnions: true,
},
],
},
// Exhaustiveness check only works for union types...
`
const day = 'Monday' as string;
Expand Down Expand Up @@ -553,6 +560,7 @@ switch (value) {
options: [
{
allowDefaultCaseForExhaustiveSwitch: true,
considerDefaultExhaustiveForUnions: true,
requireDefaultForNonUnion: false,
},
],
Expand All @@ -570,6 +578,7 @@ switch (value) {
options: [
{
allowDefaultCaseForExhaustiveSwitch: false,
considerDefaultExhaustiveForUnions: true,
requireDefaultForNonUnion: false,
},
],
Expand Down Expand Up @@ -745,6 +754,7 @@ switch (value) {
options: [
{
allowDefaultCaseForExhaustiveSwitch: true,
considerDefaultExhaustiveForUnions: true,
requireDefaultForNonUnion: true,
},
],
Expand All @@ -762,6 +772,7 @@ switch (value) {
options: [
{
allowDefaultCaseForExhaustiveSwitch: false,
considerDefaultExhaustiveForUnions: true,
requireDefaultForNonUnion: true,
},
],
Expand Down Expand Up @@ -832,8 +843,6 @@ declare const literal: 'a' | 'b';
switch (literal) {
case 'a':
break;
case 'b':
break;
default:
break;
}
Expand All @@ -857,7 +866,6 @@ switch (literal) {
options: [
{
allowDefaultCaseForExhaustiveSwitch: false,
considerDefaultExhaustiveForUnions: true,
},
],
},
Expand All @@ -876,8 +884,6 @@ switch (myEnum) {
break;
case MyEnum.Bar:
break;
case MyEnum.Baz:
break;
default: {
break;
}
Expand All @@ -895,8 +901,6 @@ declare const value: boolean;
switch (value) {
case false:
break;
case true:
break;
default: {
break;
}
Expand Down Expand Up @@ -2507,7 +2511,7 @@ switch (literal) {
],
options: [
{
considerDefaultExhaustiveForUnions: true,
considerDefaultExhaustiveForUnions: false,
},
],
},
Expand Down Expand Up @@ -2541,11 +2545,6 @@ switch (literal) {
],
},
],
options: [
{
considerDefaultExhaustiveForUnions: true,
},
],
},
{
code: `
Expand Down Expand Up @@ -2581,7 +2580,7 @@ switch (literal) {
],
options: [
{
considerDefaultExhaustiveForUnions: true,
considerDefaultExhaustiveForUnions: false,
},
],
},
Expand Down Expand Up @@ -2622,7 +2621,7 @@ switch (literal) {
],
options: [
{
considerDefaultExhaustiveForUnions: true,
considerDefaultExhaustiveForUnions: false,
},
],
},
Expand Down Expand Up @@ -2677,7 +2676,7 @@ switch (myEnum) {
],
options: [
{
considerDefaultExhaustiveForUnions: true,
considerDefaultExhaustiveForUnions: false,
},
],
},
Expand Down Expand Up @@ -2714,7 +2713,7 @@ switch (value) {
],
options: [
{
considerDefaultExhaustiveForUnions: true,
considerDefaultExhaustiveForUnions: false,
},
],
},
Expand Down
Loading