I would like to have a set of string enums in typescript so I can do things like having the user select from a set of predefined options.
I found a good solution in this answer: https://stackoverflow.com/questions/17380845/how-to-convert-string-to-enum-in-typescript
However, that only works for single words... (basically, the above allows you to get the enum name value -- and that can't have a space in it).
So, further looking, I found this person with a workaround: https://stackoverflow.com/questions/37839347/workaround-for-string-based-enums-in-typescript
That will allow something like this to work:
export enum EventType {
Photoshoot = <any>"Photoshoot",
BookingMeeting = <any>"Booking Meeting",
PostShootMeeting = <any>"Post Shoot Meeting",
Purchase = <any>"Purchase",
Print = <any>"Print"
}
The only response basically says doing so is unsafe. (but I should say, it DOES work -- I can type EventType.dot ... and Atom editor gives me the 5 camel-case options to select from (keeping things consistent in the code) I can then use that to have the string value spit out to give my users a good space-filled experience, in the future I can change the enum and for different languages/option wording without messing with the 'code' at all, etc...
Is there a better way to do this? I'd rather not do it 'unsafely' -- but I don't know of any other way.