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
interfaceA1{type: "A1";}interfaceA2{type: "A2";}typeAU=A1|A2;functionfoo(obj1: AU){constobj2: AU={type: obj1.type/* Error TS2322: Type '{ type: "A1" | "A2"; }' is not assignable to type 'AU'. Type '{ type: "A1" | "A2"; }' is not assignable to type 'A2'. Types of property 'type' are incompatible. Type '"A1" | "A2"' is not assignable to type '"A2"'. Type '"A1"' is not assignable to type '"A2"'. */};}
Expected behavior:
create a new object with the same value of the first object.
Actual behavior:
typescript error TS2322:
Type '{ type: "A1" | "A2"; }' is not assignable to type 'AU'.
Type '{ type: "A1" | "A2"; }' is not assignable to type 'A2'.
Types of property 'type' are incompatible.
Type '"A1" | "A2"' is not assignable to type '"A2"'.
Type '"A1"' is not assignable to type '"A2"'.
Daniel Rosenwasser (@DanielRosenwasser) the issue you labeled this as a duplicate of, is labeled as "intended behavior",
because { type: "A1" | "A2"; } !== { type: "A1"; } | { type: "A1" ; },
otherwise if we had more fields, we can get an unstable behaviour.
looking into it, I found that you do get unstable behavior using the fix you commented!!
if you would try to create such an object, you would normally get this error:
constau: AU={typeA: "A1",typeB: "B2"};/*Type '{ typeA: "A1"; typeB: "B2"; }' is not assignable to type 'AU'. Type '{ typeA: "A1"; typeB: "B2"; }' is not assignable to type 'A2'. Types of property 'typeA' are incompatible. Type '"A1"' is not assignable to type '"A2"'.*/
EDIT: I have issued a new issue for this new bug - #20896
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
jcalz commentedon Dec 26, 2017
Duplicate of #12052 ?
DanielRosenwasser commentedon Dec 26, 2017
Using a type assertion actually does work here.
codemau5 commentedon Dec 26, 2017
Daniel Rosenwasser (@DanielRosenwasser) the issue you labeled this as a duplicate of, is labeled as "intended behavior",
because { type: "A1" | "A2"; } !== { type: "A1"; } | { type: "A1" ; },
otherwise if we had more fields, we can get an unstable behaviour.
looking into it, I found that you do get unstable behavior using the fix you commented!!
a3 is of type AU, but is in an invalid state!!
if you would try to create such an object, you would normally get this error:
EDIT: I have issued a new issue for this new bug - #20896
typescript-bot commentedon Jan 9, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.