Skip to content

Offer auto-completions for string literal unions while writing case statements #49945

Closed
@orta

Description

@orta

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

^

Metadata

Metadata

Assignees

Labels

Domain: Completion ListsThe issue relates to showing completion lists in an editorDomain: Quick FixesEditor-provided fixes, often called code actions.Experience EnhancementNoncontroversial enhancementsSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions