Skip to content

Type inference for declared enum member assignmentΒ #52531

Closed
@molisani

Description

@molisani

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).

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions