useExhaustiveSwitchCases
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useExhaustiveSwitchCases
- This rule has an unsafe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
Description
Section titled “Description”Require switch-case statements to be exhaustive.
When working with union types in TypeScript, it’s common to want to write a switch statement intended to contain a case for each possible variant. However, if the union type changes, it’s easy to forget to modify the cases to account for any new types.
This rule reports when a switch statement over a value typed as a union of literals lacks a case for any of those literal types and does not have a default clause.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”type Day = | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday' | 'Sunday';
declare const day: Day;let result = 0;
switch (day) { case 'Monday': result = 1; break;}
type Day = | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday' | 'Sunday';
declare const day: Day;let result = 0;
switch (day) { case 'Monday': result = 1; break; case 'Tuesday': result = 2; break; case 'Wednesday': result = 3; break; case 'Thursday': result = 4; break; case 'Friday': result = 5; break; case 'Saturday': result = 6; break; case 'Sunday': result = 7; break;}
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useExhaustiveSwitchCases": "error" } } }}