Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(eslint-plugin): [switch-exhaustiveness-check] invertconsiderDefaultExhaustiveForUnions#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 ourterms of service andprivacy 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 fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 willcorrect 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)willaddress the rule's complaint.

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

Expand DownExpand 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 DownExpand 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop

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

View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand Up@@ -2507,7 +2511,7 @@ switch (literal) {
],
options: [
{
considerDefaultExhaustiveForUnions:true,
considerDefaultExhaustiveForUnions:false,
},
],
},
Expand DownExpand Up@@ -2541,11 +2545,6 @@ switch (literal) {
],
},
],
options: [
{
considerDefaultExhaustiveForUnions: true,
},
],
},
{
code: `
Expand DownExpand Up@@ -2581,7 +2580,7 @@ switch (literal) {
],
options: [
{
considerDefaultExhaustiveForUnions:true,
considerDefaultExhaustiveForUnions:false,
},
],
},
Expand DownExpand Up@@ -2622,7 +2621,7 @@ switch (literal) {
],
options: [
{
considerDefaultExhaustiveForUnions:true,
considerDefaultExhaustiveForUnions:false,
},
],
},
Expand DownExpand Up@@ -2677,7 +2676,7 @@ switch (myEnum) {
],
options: [
{
considerDefaultExhaustiveForUnions:true,
considerDefaultExhaustiveForUnions:false,
},
],
},
Expand DownExpand Up@@ -2714,7 +2713,7 @@ switch (value) {
],
options: [
{
considerDefaultExhaustiveForUnions:true,
considerDefaultExhaustiveForUnions:false,
},
],
},
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp