Skip to content

Assignability between number and enum type #10738

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

Closed
rkirov opened this issue Sep 6, 2016 · 6 comments
Closed

Assignability between number and enum type #10738

rkirov opened this issue Sep 6, 2016 · 6 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@rkirov
Copy link
Contributor

rkirov commented Sep 6, 2016

TypeScript Version: nightly (2.0.0-dev.201xxxxx)

Code

enum E {
  A,
  B
}

let n: number = 0;

let y: E = n;
let z: E|boolean = n;  // ERROR(9,5): : Type 'number' is not assignable to type 'boolean | E'.

Expected behavior:

Either disallow assigning number to E, reasonable if one considers E equivalent to 0 | 1. Or allow the assignment of number to E | boolean.

Actual behavior:

let z: E|boolean = n; // ERROR(9,5): : Type 'number' is not assignable to type 'boolean | E'.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Sep 6, 2016
@RyanCavanaugh
Copy link
Member

Regression from 1.8 behavior

@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.0.3 milestone Sep 6, 2016
@rkirov rkirov changed the title Assignability between nubmer and enum type Assignability between number and enum type Sep 6, 2016
@rkirov
Copy link
Contributor Author

rkirov commented Sep 6, 2016

Also for added mystery, changing the enum to single element enum E {A} removes the error. Admittedly, single member enums are not very common, but it might help you guys when debugging.

@sandersn
Copy link
Member

sandersn commented Sep 8, 2016

I think this is caused by the switch to enums-as-unions, which would explain why the single-member enum doesn't have the bug.

@sandersn
Copy link
Member

sandersn commented Sep 8, 2016

Also let z: E | boolean = 0 is fine too, so I think the 0 | 1 elements of the enum are incorrectly not assignable to number.

@sandersn
Copy link
Member

sandersn commented Sep 8, 2016

isSimpleTypeRelatedTo has a special case that lets numbers be assignable to enums. Looks like it (or related code) should also have a special case for union types that contain enums.

@sandersn
Copy link
Member

sandersn commented Sep 8, 2016

Fix is up at #10784

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Sep 12, 2016
@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
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants