Skip to main content

switch-exhaustiveness-check

Require switch-case statements to be exhaustive with union type.

💡

此规则报告的一些问题可以通过编辑器 建议 手动修复。

💭

该规则需要 类型信息 才能运行。


在 TypeScript 中使用联合类型或枚举时,通常需要编写一条 switch 语句,旨在为联合或枚举中的每种可能类型包含 case。 但是,如果联合类型或枚举发生更改,很容易忘记修改案例以适应任何新类型。

英: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. However, if the union type or the enum changes, it's easy to forget to modify the cases to account for any new types.

当针对作为字面量联合或枚举键入的值的 switch 语句缺少任何这些字面量类型的大小写并且没有 default 子句时,此规则会报告。

英: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.

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/switch-exhaustiveness-check": "error"
}
};
在线运行试试这个规则 ↗

选项

"allowDefaultCaseForExhaustiveSwitch"

默认为 true。 如果设置为 false,则当 switch 语句具有联合中所有内容的情况并且还包含 default 情况时,此规则也会报告。 因此,通过将此选项设置为 false,规则会变得更加严格。

英: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.

当联合类型上的 switch 语句是详尽的时,最终的 default 情况将是一种死代码形式。 此外,如果向联合类型添加新值,default 将阻止 switch-exhaustiveness-check 规则报告 switch 语句中未处理的新情况。

英:When a switch statement over a union type is exhaustive, a final default case would be a form of dead code. 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.

"allowDefaultCaseForExhaustiveSwitch" 注意事项

如果值可能具有未由联合类型表示的类型,则有时在详尽的 switch 语句中包含冗余 default 情况会很有用。 例如,在客户端和服务器之间可能存在版本不匹配的应用中,运行较新软件版本的服务器可能会发送客户端较旧类型无法识别的值。

英: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. 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.

如果你的项目有少量故意冗余的 default 案例,你可能希望为每个案例使用 内联 ESLint 禁用注释

英:If your project has a small number of intentionally redundant default cases, you might want to use an inline ESLint disable comment for each of them.

如果你的项目有许多故意冗余的 default 案例,你可能需要禁用 allowDefaultCaseForExhaustiveSwitch 并使用 default-case 核心 ESLint 规则satisfies never 检查

英:If your project has many intentionally redundant default cases, you may want to disable allowDefaultCaseForExhaustiveSwitch and use the default-case core ESLint rule along with a satisfies never check.

requireDefaultForNonUnion

默认为 false。 如果设置为 true,则当 switch 语句切换非联合类型(例如 numberstring)并且 switch 语句没有 default 情况时,此规则也会报告。 因此,通过将此选项设置为 true,规则会变得更加严格。

英: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.

这通常是可取的,以便 numberstring 交换机将受到与其他交换机相同的详尽检查。

英:This is generally desirable so that number and string switches will be subject to the same exhaustive checks that your other switches are.

此规则与 { requireDefaultForNonUnion: true } 的附加 incorrect 代码示例:

英:Examples of additional incorrect code for this rule with { requireDefaultForNonUnion: true }:

const value: number = Math.floor(Math.random() * 3);

switch (value) {
case 0:
return 0;
case 1:
return 1;
}
Open in Playground

由于 value 是非联合类型,因此仅在启用 requireDefaultForNonUnion 的情况下才要求 switch case 具有默认子句。

英:Since value is a non-union type it requires the switch case to have a default clause only with requireDefaultForNonUnion enabled.

示例

当交换机没有详尽的案例时,将其全部填写或添加默认值都可以纠正规则的投诉。

英:When the switch doesn't have exhaustive cases, either filling them all out or adding a default will correct the rule's complaint.

以下是一些使用字面量联合的代码示例:

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

type Day =
| 'Monday'
| 'Tuesday'
| 'Wednesday'
| 'Thursday'
| 'Friday'
| 'Saturday'
| 'Sunday';

declare const day: Day;
let result = 0;

switch (day) {
case 'Monday':
result = 1;
break;
}
Open in Playground

同样,以下是一些使用枚举的代码示例:

英:Likewise, here are some examples of code working with an enum:

enum Fruit {
Apple,
Banana,
Cherry,
}

declare const fruit: Fruit;

switch (fruit) {
case Fruit.Apple:
console.log('an apple');
break;
}
Open in Playground

何时不使用它

如果你不经常对具有许多部分的联合类型或枚举进行 switch,或者故意希望省略某些部分,则此规则可能不适合你。

英: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.

选项

该规则不可配置。

资源