-
Notifications
You must be signed in to change notification settings - Fork 12.8k
A flag to disable reverse mapping in number enums #57134
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
Comments
Such a flag might be problematic; suppose a declare enum FooBar {
Foo,
Bar,
} Now further suppose the library writer enabled |
For the record, the only reason string enums don't get reverse mappings is because they are vulnerable to collisions whereas numeric enums aren't. |
i believe that's not really related to my proposal. i understand that enums are the only things emitting JS (in that sense you're right) but it's not my proposal which will solve the issue of mismatch of tsconfig between built lib and source. for example, i wonder if - to some extend - a library with types compiled with |
Right, but I'm pointing it out because the potential for such mismatch to be a runtime footgun is very likely to be cited as a reason for the TS devs to reject the proposal. |
this i know, but there are valid reasons to use number has enums. from storing in db, serialization concerns to magic calculations ; doesn't mean it should not be first class citizen |
Essentially sounds like a duplicate of #44753 to me. |
And a maintainer already noticed the above issue too: #44753 (comment) |
#37282 is very specifically about |
If you don't want the reverse lookup, there's not much reason left to write a numeric enum. const MyEnum = {
x: 1,
y: 2
} as const;
type MyEnum = (typeof MyEnum)[keyof typeof MyEnum]; |
π Search Terms
"number" "enum" "Object.values"
β Viability Checklist
β Suggestion
A simple configuration flag to disable explicitly reverse mapping for enums (#30487)
While this is intended behavior, it can be found as unnatural behavior since string enums don't behave the same way. I don't mind the current behavior, but having a flag to align both number and string enums would make sense, at least to me.
π Motivating Example
playground link
π» Use Cases
What do you want to use this for?
writing typeguards in the same way for string and number enums
What shortcomings exist with current approaches?
there's a need for a filtering pass on enum set
What workarounds are you using in the meantime?
playground link
Other considerations
Maybe another approach would be to have a different way of writing enums so that they match
instanceof
but i can't think of anything which is not ugly and hacky.Or since enums emit JS code, have their own
.is()
type guard function generated would be nice too.The text was updated successfully, but these errors were encountered: