Skip to content
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

Closed
omeid opened this issue Feb 3, 2016 · 4 comments
Closed

Implicit Enum Duplicate Values #6864

omeid opened this issue Feb 3, 2016 · 4 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@omeid
Copy link

omeid commented Feb 3, 2016

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:

enum Color {Red = 3, Green = 2, Blue};

Color.Red == Color.Blue; //true
@Arnavion
Copy link
Contributor

Arnavion commented Feb 3, 2016

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.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Feb 3, 2016
@RyanCavanaugh
Copy link
Member

I'm not sure which would be more surprising. I'll bring it up.

@omeid
Copy link
Author

omeid commented Feb 3, 2016

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.

@RyanCavanaugh RyanCavanaugh added Too Complex An issue which adding support for may be too complex for the value it adds and removed In Discussion Not yet reached consensus labels Mar 15, 2016
@RyanCavanaugh
Copy link
Member

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.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

3 participants