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
From section 3.11.4 (assignment compatibility) of the specification, an object type T should be assignable from an object type S when all T's properties exist on type S (same name) and each can be assigned from the corresponding property on type S.
However this does not work when property assignability was proven through a type-guard.
TypeScript Version: 2.5.2
Code
interfaceWidget{readonlydata: string|number;}interfaceGadget{readonlydata: string;}functionfoo(g: Gadget): void{console.log(g.data);}functionbar(w: Widget): void{if(typeofw.data==="string"){constd: string=w.data;// `w.data` correctly recognized as a `string`foo(w);// however `w` cannot be used as a `Gadget`}}
Expected behavior:
Compiles successfully.
Actual behavior:
Compilation fails with:
error TS2345: Argument of type 'Widget' is not assignable to parameter of type 'Gadget'.
Types of property 'data' are incompatible.
Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
The text was updated successfully, but these errors were encountered:
From section 3.11.4 (assignment compatibility) of the specification, an object type
T
should be assignable from an object typeS
when allT
's properties exist on typeS
(same name) and each can be assigned from the corresponding property on typeS
.However this does not work when property assignability was proven through a type-guard.
TypeScript Version: 2.5.2
Code
Expected behavior:
Compiles successfully.
Actual behavior:
Compilation fails with:
The text was updated successfully, but these errors were encountered: