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
// A *self-contained* demonstration of the problem follows...functiontestUnion(){typeX="A"|"B";functiontest(x: X){if(x==="A"){constcopyA: "A"=x;// succeeds, x's type is "A"// const copyB: "B" = x; // error}}}functiontestExtends(){typeX="A"|"B";functiontestZ<ZextendsX>(x: Z){if(x==="A"){constcopyA: "A"=x;// error, x's type is still Z extends "A" | "B"// const copyB: "B" = x; // error, as expected}}}
Expected behavior:
The initialization of copyA in testExtends should succeed.
Actual behavior:
src/test.ts(17,19): error TS2322: Type 'Z' is not assignable to type '"A"'.
Type 'X' is not assignable to type '"A"'.
Type '"B"' is not assignable to type '"A"'.
This prevents some useful generic implementations where the actual type of X is a keyof type expression, and other types are resolved based on the type of x.
The text was updated successfully, but these errors were encountered:
TypeScript Version: 2.4.0
Code
Expected behavior:
The initialization of
copyA
in testExtends should succeed.Actual behavior:
This prevents some useful generic implementations where the actual type of X is a keyof type expression, and other types are resolved based on the type of x.
The text was updated successfully, but these errors were encountered: