Skip to content

String enum by default #33015

Closed
Closed
@garrettmaring

Description

@garrettmaring

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.

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