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
Expected behavior:
there should be no type error Actual behavior:
Type '{ type: Types; variable: string; }' is not assignable to type 'ITypeA | ITypeB'.
Type '{ type: Types; variable: string; }' is not assignable to type 'ITypeB'.
Types of property 'type' are incompatible.
Type 'Types' is not assignable to type 'Types.TYPE_B'.
This code should work same as, because there is no logical distinction between them
You're asking that we treat types of the form { kind: 'a' | 'b', ... } as assignable to types of the form { kind: 'a', ... } | { kind: 'b', ... }. This is a valid assumption only in the narrow cases of a single property differing and the remaining properties of the object types all being identical. Those situations are rare in practice, which is why we don't to this extra work in the checker right now.
A simple workaround is to replace ITypeA | ITypeB with single declaration:
I actually ran into this a lot while working on #18285 - you run into more than you'd expect once you start working with discriminated unions and the fields you pick from them during control flow analysis... and it's frustrating when it happens.
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
Code
Expected behavior:
there should be no type error
Actual behavior:
This code should work same as, because there is no logical distinction between them
The text was updated successfully, but these errors were encountered: