Implicit Enum Duplicate Values #6864
Description
Activity
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.
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptIn DiscussionNot yet reached consensusNot yet reached consensus
on Feb 3, 2016 RyanCavanaugh commented
on Feb 3, 2016 MemberMore actionsI'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.
- addedToo ComplexAn issue which adding support for may be too complex for the value it addsAn issue which adding support for may be too complex for the value it addsand removedIn DiscussionNot yet reached consensusNot yet reached consensus
on Mar 15, 2016 RyanCavanaugh commented
on Mar 15, 2016 MemberMore actionsFinally 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.
- locked and limited conversation to collaborators
on Jun 19, 2018
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: