-
Notifications
You must be signed in to change notification settings - Fork 14
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
Enum decoders are cumbersome and not intuitive #18
Comments
@mulias This says that "enums are real objects at runtime" https://www.typescriptlang.org/docs/handbook/enums.html, so I would expect |
@RocketPuppy I had looked into this a while ago and had forgotten some of the details. The issue I found is that if you define an I should note that shortly after posting this issue I realized that it's totally possible to write the |
Ah, I thought it might have been an issue with const enums. You could have two decoders. One that takes an enum object and one that takes enum members. I think Typescript should prevent you from using the const enum with the enum object decoder. |
Only one way to find out :) |
New issue with
Enum runtime objects are implemented as two way mappings, so if all I have is the enum then I can't actually tell which values are the keys. All the ways around that seem pretty hacky, so I think we're stuck with listing out the enum values. |
It appears that the behavior is heavily intended, and is also specific to numeric values for enums. If the enum uses string values, no reverse mapping happens - so at least for string enums, it becomes possible to make the assumption that you can coerce the enum into an array and use an Array.includes() call to verify if the enum is a member. However, there's no easy way to verify if the enum is a string enum or not, making it something that can only be done by hand. More info here: https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings |
Given a TS enum
We currently write the decoder as
which isn't great.
It's worth noting that the most ideal syntax is not possible, since TS compiles out enums and hardcodes the enum values during compilation
Something like this should be possible:
But no matter how I bash at the types I am unable to get the type checker to reconcile the relationship between the elements of the enum and the enum in total.
The text was updated successfully, but these errors were encountered: