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

Type inference for declared enum member assignment #52531

Closed
molisani opened this issue Jan 31, 2023 · 1 comment Β· Fixed by #52542
Closed

Type inference for declared enum member assignment #52531

molisani opened this issue Jan 31, 2023 · 1 comment Β· Fixed by #52542
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@molisani
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

enum, declare enum, union enum

πŸ•— Version & Regression Information

  • This changed between versions 4.9 and 5.0

Noticed this change when testing the 5.0 beta, specifically in the wake of #50528.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

enum MyEnum { A, B, C }

let val1 = MyEnum.A;
//  ^? MyEnum
val1 = MyEnum.B;

declare enum MyDeclaredEnum { A, B, C }

let val2 = MyDeclaredEnum.A;
//  ^? MyDeclaredEnum.A
val2 = MyDeclaredEnum.B;
// ^ Type 'MyDeclaredEnum.B' is not assignable to type 'MyDeclaredEnum.A'.(2322)

πŸ™ Actual behavior

Assigning a declared enum member to a variable with let infers the type to be exactly the type of that specific enum member.

πŸ™‚ Expected behavior

The declared enum assignment should be the same as the regular enum assignment, where the type is inferred as the enum itself (a union of all enum members).

@ahejlsberg ahejlsberg self-assigned this Jan 31, 2023
@ahejlsberg ahejlsberg added the Needs Investigation This issue needs a team member to investigate its status. label Jan 31, 2023
@ahejlsberg
Copy link
Member

In a declaration context (a declare declaration or in a .d.ts file), an enum member without an initializer is considered a computed enum member. Previously, such members caused an enum to become a non-union numeric enum type where there aren't distinct types for each member. For example, in

declare enum E { A, B, C }

the members would all have the same type E. Following #50528 we now create unique and opaque enum member types E.A, E.B, and E.C and consider E a union of those types. This is analogous to what happens when the members are initialized with literal values. However, we don't have "fresh" and "regular" forms of the unique enum member types, and therefore they don't widen. So, where E.A widens to E (which is the union E.A | E.B | E.C) when E.A is declared with a literal value, it doesn't when it is a computed member. Looks like we need to fix this by having "fresh" and "regular" forms of computed enum member types.

@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Feb 1, 2023
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants