-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Enum with computed values, causes weird bug in union type exhaustion #18393
Comments
Simpler case: enum E {
A = 2,
B = 2 << 1 // <- bit shifted value
}
function assertNever(obj: never, msg?: string): never {
throw new Error(msg || `Unsupported option: ${obj}`);
}
function resolve(o: E) {
switch (o) {
case E.A: return 1;
case E.B: return 2;
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: const enum E {
A = +1,
B = 2,
} |
Nice catch, I updated the issue description to basically use your case. |
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: