Skip to content

String enum by default #33015

Closed
Closed

Description

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.

Activity

  1. jack-williams commented on Aug 21, 2019

    @jack-williams
    Collaborator

    Does this match your suggestion? #16464

  2. geraldarthurdavis commented on Aug 21, 2019

    @geraldarthurdavis
    Author

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

  3. typescript-bot commented on Aug 30, 2019

    @typescript-bot
    Contributor

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

  4. typescript-bot commented on Sep 2, 2019

    @typescript-bot
    Contributor

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

  5. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions