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
Type narrowing does not appear to cover the cases where a const enum member's value is not a literal.
constenumConstEnum{foo=2|1,}functionf(e: ConstEnum): number{switch(e){caseConstEnum.foo:
return0;default:
assertNever(e);}}exportfunctionassertNever(x: never): never{thrownewError('Expected never to have value '+x);}
This code will result in the following error: Argument of type 'ConstEnum' is not assignable to parameter of type 'never'. However, the switch should be covering all cases, and therefore I would expect the type of e to be never in the default case. Changing the value of foo to a literal by removing arithmetic results in the expected behavior (no error). It looks like the compiler is properly doing the arithmetic at compile-time to compute 2 | 1 = 3.
The text was updated successfully, but these errors were encountered:
Enums can be used to mean a set of literal values, or a set of flags. for flags doing an exhaustive check is not correct. the compiler uses the existence of an intializer + expression as an indication that this is a flag enum, and disables narrowing. enabling narrowing on non-literal enums would be a breaking change.
Ah I see. I agree with you with respect to type narrowing being suspicious when you have a flags enum. Unfortunately it seems like using the existence of an expression initializer will produce both false positives (just because I use an expression does not indicate I necessary intend to combine the values) and false negatives (some people specify the flags values as literals). Did this decision take place on GitHub, do you happen to have a link to the PR?
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Type narrowing does not appear to cover the cases where a const enum member's value is not a literal.
This code will result in the following error:
Argument of type 'ConstEnum' is not assignable to parameter of type 'never'.
However, the switch should be covering all cases, and therefore I would expect the type ofe
to benever
in the default case. Changing the value offoo
to a literal by removing arithmetic results in the expected behavior (no error). It looks like the compiler is properly doing the arithmetic at compile-time to compute2 | 1 = 3
.The text was updated successfully, but these errors were encountered: