Repository navigation
Closed
Labels
Domain: LS: Completion ListsThe issue relates to showing completion lists in an editorDomain: LS: Quick FixesEditor-provided fixes, often called code actions.Experience EnhancementNoncontroversial enhancementsSuggestionAn idea for TypeScript
Milestone
Description
Suggestion
declare const example: "one" | "two" | "three"
switch(example) {
cas|
} Offer in suggestions: 'case (add missing cases)'. Which would turn into:
declare const example: "one" | "two" | "three"
switch(example) {
case: "one":
// ...
break
case: "two":
// ...
break
case: "three":
// ...
break
} Extra credit: add two options 'add missing cases' and 'add exhaustive missing cases' which adds a never in the default:
declare const example: "one" | "two" | "three"
switch(example) {
case: "one":
// ...
break
case: "two":
// ...
break
case: "three":
// ...
break
default: {
const exhaustiveCheck: never = value;
throw new Error(exhaustiveCheck);
}
} declare const example: "one" | "two" | "three"
switch(example) {
case: "one": console.log("one") break;
cas|
} Offer in suggestions:
- 'case (fill in missing cases)'
🔍 Search Terms
switch autocomplete switch suggestions switch fill case string union
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
📃 Motivating Example
I got me a long string literal enum, and I've got to cover all those cases.
💻 Use Cases
^
Reacted by Bruce Pascoe, Daniel Rosenwasser, Thundercraft5 and Bart Lens
Reacted by Oleksandr Tarasiuk, uhyo, Daniel Rosenwasser, Artem Tyurin, Thundercraft5 and Bart Lens
Metadata
Metadata
Assignees
Labels
Domain: LS: Completion ListsThe issue relates to showing completion lists in an editorDomain: LS: Quick FixesEditor-provided fixes, often called code actions.Experience EnhancementNoncontroversial enhancementsSuggestionAn idea for TypeScript
Activity
fatcerberus commentedon Jul 19, 2022
🚲 🏠
I like the idea of having the option to add the default case; I'm not, however, a fan of "add exhaustive missing cases" as the name of the fix, since in reality both versions are exhaustive, the latter just adds an explicit check for exhaustiveness. If I see both fixes presented to me, I'm immediately going to wonder "What's the difference? Is the non-exhaustive version going to miss some cases or something?"
orta commentedon Jul 19, 2022
This is a good argument 👍🏻 , it could be "add missing cases with never check'
heroboy commentedon Sep 23, 2022
try this: https://marketplace.visualstudio.com/items?itemName=heroboy.generate-switch-case-for-enum-type

gabritto commentedon Jan 16, 2023
Related: #50996 offers something along those lines, but only as a completion item.
DanielRosenwasser commentedon Mar 21, 2024
I think #50996 just fixed this in 5.0, right?