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

String enum by default #33015

Closed
3 tasks done
garrettmaring opened this issue Aug 21, 2019 · 4 comments
Closed
3 tasks done

String enum by default #33015

garrettmaring opened this issue Aug 21, 2019 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@garrettmaring
Copy link

Search Terms

enum,string,string enum

Suggestion

I find myself often writing something like this:

enum ButtonType {
  Primary = "Primary",
  Secondary = "Secondary",
}

I believe some simple syntactic sugar around the above code would slightly boost productivity for those using TypeScript.

Use Cases

For my use cases, I write most enums in the above fashion. Aside from debugging, my most common case is generating HTML tag class names from an enum.

Take the popular classnames package from Jed Watson. The documentation shows the following example:

let buttonType = 'primary';
classNames({ [`btn-${buttonType}`]: true });

In TypeScript:

enum ButtonType {
  Primary = "Primary",
  Secondary = "Secondary",
}
let buttonType = ButtonType.Primary
classNames({ [`btn-${buttonType.toLowerCase()}`]: true });

Examples

I have no good ideas about what the syntax for this would look like. This suggestion comes purely from my experience programming in TypeScript and not from any principles (or lack thereof) of language design.

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. if opted in, the generated values would have strings(string enums)
  • [-] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.) if opted in, the enum values would have strings(string enums)
  • This feature would agree with the rest of TypeScript's Design Goals.
@jack-williams
Copy link
Collaborator

Does this match your suggestion? #16464

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 21, 2019
@garrettmaring
Copy link
Author

@jack-williams ah, it does. My searching failed me. We can close this ticket as a duplicate as there has already been discussion there.

@microsoft microsoft deleted a comment from typescript-bot Aug 28, 2019
@microsoft microsoft deleted a comment from typescript-bot Aug 28, 2019
@microsoft microsoft deleted a comment from typescript-bot Aug 28, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

1 similar comment
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants