-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Specific enum value not comparable with number in 5.0 #52701
Comments
@jdom I filed this based on your TS discussion email. |
fwiw the example given feels like a misuse of enums. If anything that should be a namespace instead. |
I don't quite understand TS enums (I don't use them in my own code) - so I have no idea if this should work or not. If it should, I can create a PR patching my change to allow this. |
Generally the purpose of enums is that you have a set of symbolic constants whose exact value is unimportant (except maybe for debugging); all thatβs important is that |
The fact that this enum has mixed constant (?) values ( declare const val: string | number
declare var someValue: number
if (someValue > val) { // errors
someValue = 100;
} |
I generally agree that these "constant bags" are probably a misuse of enums, though I suspect in this case it's being used for the inlining provided by const-ness. |
I would disagree; this code example references a specific enum member which is a number and is interchangable with the literal 1000. It seems to me like this is a bad interaction of #52048 and #50528 where the freshness of this enum reference is being lost in the comparison check. |
Sure, but even with inlining - we end up comparing |
Oh, I see what you are saying here - ye. I guess that perhaps this might be a bug worth fixing then. I would love to have some extra confirmation before I jump into fixing this though. |
I can provide details about the reason for doing this, and is like @jakebailey mentioned: it's because it allows me to define some constants on the top of the file that I just reuse in my code, and will automatically be inlined in the generated javascript. Perhaps there's a better way to have such inlining without using |
In this case, I think this might just be something we didn't have a test for, so wasn't caught. The error goes away if we don't But generally speaking, having a grab bag of random unrelated constants in an enum is not a super good idea; if you split them apart into smaller, related pieces, you would avoid this. (I have further const enum inlining opinions but they're sorta moot and hypocritical given TS itself makes heavy use of them exactly for the perf of inlining...) |
More correctly would be to have a variant of the above which checks |
I've sent #52703 for discussion. |
Per design meeting, this is a bug and we should allow this code. Just need to get the above PR reviewed and fixed up. |
Bug Report
π Search Terms
operator cannot be applied enum number comparison
π Version & Regression Information
β― Playground Link
Playground Link
π» Code
π Actual behavior
Removing the string-ish value from
Constants
stops the error.π Expected behavior
No error; even though
Constants
shouldn't be comparable thanks to #52048, this seems like it should work because it's only referencing a specific value./cc @Andarist @ahejlsberg
The text was updated successfully, but these errors were encountered: