Skip to content

Numeric enums are assignment compatible with number #26443

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
mprobst opened this issue Aug 14, 2018 · 2 comments
Closed

Numeric enums are assignment compatible with number #26443

mprobst opened this issue Aug 14, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@mprobst
Copy link
Contributor

mprobst commented Aug 14, 2018

TypeScript Version: Version 3.1.0-dev.20180813

Search Terms: enum number assignment compatible subtype

Code

enum NumericEnum { A = 0, B, C }
enum OtherNumericEnum { A = 0, B, C }
enum StringEnum { D = 'd', E = 'e', F = 'f' }

// This compiles (but should not)
let f: NumericEnum = 0;
let f2: NumericEnum = 123 /* not even a member */;
let f3: OtherNumericEnum = 0;

function takesNumericEnum(f: NumericEnum) {}
takesNumericEnum(123);

// This fails to compile as expected:
let b: StringEnum = 'g';
let b1: StringEnum = 'd';
f = f3;
takesNumericEnum(f3);

function takesStringEnum(f: StringEnum) {}
takesStringEnum('g');

Expected behavior:

Numeric enums should not be assignable from number.

Actual behavior:

They are.

Playground Link:

http://www.typescriptlang.org/play/#src=enum%20NumericEnum%20%7B%20A%20%3D%200%2C%20B%2C%20C%20%7D%0Aenum%20OtherNumericEnum%20%7B%20A%20%3D%200%2C%20B%2C%20C%20%7D%0Aenum%20StringEnum%20%7B%20D%20%3D%20'd'%2C%20E%20%3D%20'e'%2C%20F%20%3D%20'f'%20%7D%0A%0A%2F%2F%20This%20is%20ok%20(but%20should%20not%20be)%0Alet%20f%3A%20NumericEnum%20%3D%200%3B%0Alet%20f2%3A%20NumericEnum%20%3D%20123%20%2F*%20not%20even%20a%20member%20*%2F%3B%0Alet%20f3%3A%20OtherNumericEnum%20%3D%200%3B%0A%0Afunction%20takesNumericEnum(f%3A%20NumericEnum)%20%7B%7D%0AtakesNumericEnum(123)%3B%0A%0A%2F%2F%20But%20this%20is%20not.%0Alet%20b%3A%20StringEnum%20%3D%20'g'%3B%0Alet%20b1%3A%20StringEnum%20%3D%20'd'%3B%0Af%20%3D%20f3%3B%0AtakesNumericEnum(f3)%3B%0A%0Afunction%20takesStringEnum(f%3A%20StringEnum)%20%7B%7D%0AtakesStringEnum('g')%3B%0A%0A

Related Issues:

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 14, 2018
@RyanCavanaugh
Copy link
Member

Duplicate #26362 and many others

@mprobst
Copy link
Contributor Author

mprobst commented Aug 14, 2018 via email

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