Skip to content

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

Closed

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

^

Activity

  1. fatcerberus commented on Jul 19, 2022

    @fatcerberus

    🚲 🏠

    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?"

  2. orta commented on Jul 19, 2022

    @orta
    ContributorAuthor

    This is a good argument 👍🏻 , it could be "add missing cases with never check'

  3. gabritto commented on Jan 16, 2023

    @gabritto
    Member

    Related: #50996 offers something along those lines, but only as a completion item.

  4. DanielRosenwasser commented on Mar 21, 2024

    @DanielRosenwasser
    Member

    I think #50996 just fixed this in 5.0, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions