String Enum Initializer
See original GitHub issueString 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:
- Created 6 years ago
- Reactions:34
- Comments:38 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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”.
like this?