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

Any number can be assigned to enum member type #22311

Closed
rlz opened this issue Mar 3, 2018 · 4 comments
Closed

Any number can be assigned to enum member type #22311

rlz opened this issue Mar 3, 2018 · 4 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@rlz
Copy link

rlz commented Mar 3, 2018

Search Terms:
enum assign number

Code

enum X {
  A, B
}

const x: X.A = 5;

const y: X.A = X.B;

Expected behavior:
2 errors: when assigning number literal and wrong enum member.

Actual behavior:
Typescript allows assigning any number to specific member type.

Playground Link:
http://bit.ly/2CZsUrK

@DanielRosenwasser DanielRosenwasser changed the title Any number can be assigning to enum member type Any number can be assigned to enum member type Mar 3, 2018
@DanielRosenwasser
Copy link
Member

I was very close to marking this as a duplicate (mostly with #6131 in mind, but also sort of #8020 - see #8020 (comment)).

The key thing here is that we're working with an individual enum member type, not just an enum type.

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript In Discussion Not yet reached consensus Bug A bug in TypeScript and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Mar 3, 2018
@rlz
Copy link
Author

rlz commented Mar 3, 2018

Yes, I expected member type is 0 in this case, so I should be able to assign 0 but not 5.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Bug A bug in TypeScript labels Mar 3, 2018
@RyanCavanaugh
Copy link
Member

So we'd allow e.g. const x: X.A = 5 + 0 but not = 5 ?

@RyanCavanaugh
Copy link
Member

The key thing here is that we're working with an individual enum member type, not just an enum type.

The problem here is that X is just an alias for X.A | X.B, so any argument about assignability to X.A applies equally to assignability to X.

Given the back compat constraints and unlikelihood of finding a bug in userland, keeping the current behavior seems like the best choice of action.

@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision and removed In Discussion Not yet reached consensus labels Aug 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants