-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Implicit Enum Duplicate Values #6864
Comments
I understand it may be surprising, but it's also how enums work in C / C++ / C# so there is precedent for it. If someone wrote that enum specifically with the intention to have Blue === 3, they would not want TS to warn or error about it. |
I'm not sure which would be more surprising. I'll bring it up. |
I understand the precedent argument but is that the better option? I would argue that if for: enum Color {Red = 3, Green = 2, Blue, Ultraviolet};
Color.Red == Color.Blue; //true The compiler complained about implicit duplicate values and the fix was this: enum Color {Red = 3, Green = 2, Blue = 3, Ultraviolet};
Color.Red == Color.Blue; //true Or better yet, enum Color {Red = 3, Green = 2, Blue = Color.Red, Ultraviolet};
Color.Red == Color.Blue; //true Or even better: enum Color {Red = 3, Green = 2, Blue = Red, Ultraviolet};
Color.Red == Color.Blue; //true It would be more clear and hard to miss any likely bug. |
Finally got to this. Combination of too expensive (every member has to check every other member, basically), too rare an error to make, and too precedented in other languages with implicitly-numbered enums. |
Enum value incrementation does not consider previously defined values, nor does the compiler throws an error on duplicate values.
Which means you can end up with potential bugs:
The text was updated successfully, but these errors were encountered: