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. added
    Domain: LS: Completion Lists
    Suggestion
    In Discussion
    on Jul 19, 2022
  4. added
    Experience Enhancement
    and removed
    In Discussion
    on Oct 14, 2022
  5. gabritto commented on Jan 16, 2023

    @gabritto
    Member

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

  6. added
    Domain: LS: Quick Fixes
    on May 15, 2023
  7. 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

Domain: LS: Completion ListsDomain: LS: Quick FixesExperience EnhancementSuggestion

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions