-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
keyOf enums + Enum classes #80
Comments
Hi @michahell! Can you help explain the use case for modelling enums as classes? Today jstt supports 2 types of enums:
What is the advantage of your approach over (2)? What do you think the corresponding JSONSchema should look like? |
hello @bcherny ! The only advantage of my last proposed approach over (2) is that one can specify any of the enums by using static constants, instead of using (what I call) 'magic strings'. I think that ^ is an improvement because:
|
Those are both good points! I assume there should be a reverse mapping too (ie. 'black' -> 'BLACK')? |
Hmm... Haven't thought about reverse mapping, that could prove useful as well, but not sure how to do that off the top of my head |
@michahell A few notes, looking into this more:
Finally, it looks like proper string enums will be available in TS2.4 - microsoft/TypeScript#15486. Both the literal union (1) and class-based (2) solutions are lacking, and the namespace (3) solution seems like a hack. I'm going to close this issue in anticipation of TS2.4. Feel free to reopen if you have strong objections. |
Agreed @bcherny! Hadn't looked at the TypeScript roadmap before properly, thanks for investigating! |
We use a custom made json-schem to TS script which creates
TitleCasedEnums
classes withpublic static string = 'enumValue'
fields. This lacks actual Enum behaviour but does provide for easy enum accessibility. We were looking into enhancing this with the TS 2.1keyof
property.Right now, we have something like this:
This would already be a lot better:
But this would be best:
The text was updated successfully, but these errors were encountered: