question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

String Enum Initializer

See original GitHub issue

String Enum Initializer

A more typesafe and succinct way of defining enum types that compile to strings

Search Terms

enum, string, string enum default value initializer infer

Proposal

It seems like you should be able to specify that you are using a string enum, and then you’d only have to write out the ‘value’ once, and it would be automatically made into a string matching the enum code.

enum<string> Action {
   LOAD_PROFILE,
   ADD_TASK,
   REMOVE_TASK
}

or

enum Action: string {
   LOAD_PROFILE,
   ADD_TASK,
   REMOVE_TASK
}

instead of

enum Action {
   LOAD_PROFILE = "LOAD_PROFILE",
   ADD_TASK = "ADD_TASK",
   REMOVE_TASK = "REMOVE_TASK"
}

Thanks for all the great work!

EDIT Recently updated to reflect variations proposed by @imcotton here and @lostpebble here; as well as search terms proposed by @KennethKo (#36319) and @garrettmaring (#33015)

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:34
  • Comments:38 (6 by maintainers)

github_iconTop GitHub Comments

11reactions
RyanCavanaughcommented, Jun 6, 2018

We’re always looking at things holistically. There are over 1,000 open suggestions and it would be a complete disaster to just do all of them; features need to more than pay for themselves in terms of new complexity vs value added.

We have to consider questions like:

  • Does this enable you to do something you couldn’t do before?
    • No - it’s pure sugar
  • If not, is the existing way of doing it truly burdensome, or just a bit annoying?
    • It’s just annoying
  • How many people are affected by the feature not existing?
    • The answer appears to be 13ish?
  • Could this be solved in some other way, such as tooling?
    • “Convert numeric enum to string enum” refactoring would be trivial to write; we should consider doing that instead
  • Is there strong precedent from other languages implying a need here?
    • No
  • Are there frameworks or libraries whose patterns are greatly simplified by it?
    • No (?)

The bar is much higher than you probably think it is - once something goes into the language, we can’t take it out, and everything that exists is something that future TS learners will need to understand. Features at the language level need to feel like “the thing we’ve been missing all along”, not “this would be nice”.

9reactions
imcottoncommented, Jul 25, 2017

like this?

enum Action: string {
    LOAD_PROFILE,
    ADD_TASK,
    REMOVE_TASK,
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Overriding Enum init?(rawValue: String) to not be optional
The default initializer is failable . It means that if the received parameter does not match a valid enum case it does return...
Read more >
Swift Tip: Enum Initializers - objc.io
In Swift Talk 45, we wrote a small library for parsing routes. We represented routes as enums, and added a custom initializer to...
Read more >
init(rawValue:) | Apple Developer Documentation
Initializer. init(rawValue:) Creates a new instance with the specified raw value. iOS 8.0+ iPadOS 8.0+ macOS 10.10+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS...
Read more >
Enum Raw Values and Failable Initializers - Use Your Loaf
Each case of a Swift enum can be assigned a raw value. The raw values all have to be of the same type...
Read more >
TypeScript string enums, and when and how to use them
In summary, for constant enums, the enum member value can be the first member without an initializer or must have an initializer which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found