You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
enumE{A=2,B=2<<1// <- computed value}functionassertNever(obj: never,msg?: string): never{thrownewError(msg||`Unsupported option: ${obj}`);}functionresolve(o: E){switch(o){caseE.A: return1;caseE.B: return2;default:
assertNever(o);// Argument of type 'E' is not assignable to parameter of type 'never'.}}
Expected behavior:
The compiler should recognize that the types of o has been exhausted,
even though the enum has a bit shifted value.
Actual behavior: Compile error:
Argument of type 'E' is not assignable to parameter of type 'never'.
The text was updated successfully, but these errors were encountered:
enumE{A=2,B=2<<1// <- bit shifted value}functionassertNever(obj: never,msg?: string): never{thrownewError(msg||`Unsupported option: ${obj}`);}functionresolve(o: E){switch(o){caseE.A: return1;caseE.B: return2;default:
assertNever(o);// Argument of type 'E' is not assignable to parameter of type 'never'.}}
I think the problem may be that TypeScript is not diving into computed enum. For example, the following enum also triggers this:
constenumE{A=+1,B=2,}
DanielRosenwasser
changed the title
Enum with bit-shifted values, causes weird bug in union type exhaustion
Enum with computed values, causes weird bug in union type exhaustion
Sep 11, 2017
Under the covered there are really two kinds of enums, computed and union. This is needed to allow narrowing for enums/ treat them as unions, without breaking flag enums. having any expression in an enum initializer makes the enum behave as a flag enum (i.e. an open subset of the number domain) rather than a union enum (a collection of unit types)
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: 2.5.2
Working:
Not working:
Expected behavior:
The compiler should recognize that the types of
o
has been exhausted,even though the enum has a bit shifted value.
Actual behavior:
Compile error:
The text was updated successfully, but these errors were encountered: