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

Numeric enums may be not be type safe #53562

Closed
vituchon opened this issue Mar 28, 2023 · 3 comments
Closed

Numeric enums may be not be type safe #53562

vituchon opened this issue Mar 28, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@vituchon
Copy link

vituchon commented Mar 28, 2023

HI there, how you doing guys? Hope fine.

I was studying a little bit the diff between using enums vs (union types + literal types) on this post at stackoverflow.

A given person states the following in his answer

  1. Numeric enums are not type safe
    You can assign any number to numeric enums.
 enum ZeroOrOne {
  Zero = 0,
  One = 1
}
const zeroOrOne: ZeroOrOne = 2; // no error!!

Version
image

As far I have work with enums I can not assing any literal value (Regarless the type...) so the error would be even this const zeroOrOne: ZeroOrOne = 1; as the literal value is not an enum value.

Perhaphs this is something you already know about, but I don't know and I think it is okey to report this here as an issue.

Thanks for reading... forgive me if I'm wrong in something.

Greetings.

Víctor.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 28, 2023
@RyanCavanaugh
Copy link
Member

See #26362, etc

@RyanCavanaugh RyanCavanaugh closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 2023
@vituchon
Copy link
Author

Thanks @RyanCavanaugh for the quick answer. I see there is tradeoff here. Dificult chooise.. or you can have bitwise operations enums and strict enums, and everybody happy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants