Skip to content

Mixed-valued literal enums assignability issue #18082

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
zhuravlik opened this issue Aug 28, 2017 · 4 comments
Closed

Mixed-valued literal enums assignability issue #18082

zhuravlik opened this issue Aug 28, 2017 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@zhuravlik
Copy link

TypeScript Version: 2.6 (just checked out)

Code

enum E {
  A = 1,
  B = "2"
}

var q: E.A = 1; // okay
var z: E.B = "2"; // error TS2322: Type '"2"' is not assignable to type 'E.B'

Expected behavior:
In both cases assignability should work the same way.

Actual behavior:
Assignability passes with numeric literal enum values, but doesn't pass with string literal enum values

@zhuravlik
Copy link
Author

By the way, looks like typechecking for numeric items in such mixed enums doesn't work at all
var q: E.A = 5; // still okay

@zhuravlik zhuravlik changed the title String-valued literal enums assignability issue Mixed-valued literal enums assignability issue Aug 28, 2017
@ikatyang
Copy link
Contributor

var z: E.B = "2"; // error TS2322: Type '"2"' is not assignable to type 'E.B'

This one seems duplicate of #16851.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Aug 28, 2017
@mhegazy mhegazy added this to the TypeScript 2.6 milestone Aug 28, 2017
@mhegazy mhegazy added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript labels Aug 28, 2017
@mhegazy mhegazy removed this from the TypeScript 2.6 milestone Aug 28, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Aug 28, 2017

The numeric values allowing numbers is there to support bit flags. it is a common use case of enums, and changing that would be a massive break. see similar discussion in #17734.

The string values behave as i would expect. enums are "tagged" versions of the string literals. the enum member type is assignable to the string literal type but not vice versa, other wise rename operations are not guaranteed to work.

@zhuravlik
Copy link
Author

@mhegazy Thanks a lot for the explanation.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants