Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
5 tasks done
orta opened this issue Jul 19, 2022 · 5 comments
Closed
5 tasks done
Assignees
Labels
Domain: Completion Lists The issue relates to showing completion lists in an editor Domain: Quick Fixes Editor-provided fixes, often called code actions. Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript

Comments

@orta
Copy link
Contributor

orta commented Jul 19, 2022

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

^

@fatcerberus
Copy link

🚲 🏠

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
Copy link
Contributor Author

orta commented Jul 19, 2022

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

@DanielRosenwasser DanielRosenwasser added Domain: Completion Lists The issue relates to showing completion lists in an editor Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Jul 19, 2022
@heroboy
Copy link

heroboy commented Sep 23, 2022

@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements and removed In Discussion Not yet reached consensus labels Oct 14, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Oct 14, 2022
@gabritto
Copy link
Member

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

@gabritto gabritto self-assigned this Jan 16, 2023
@DanielRosenwasser DanielRosenwasser added the Domain: Quick Fixes Editor-provided fixes, often called code actions. label May 15, 2023
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Mar 21, 2024

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
Labels
Domain: Completion Lists The issue relates to showing completion lists in an editor Domain: Quick Fixes Editor-provided fixes, often called code actions. Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants