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
{{ message }}
Repository navigation
type guard does not narrow intersection over union #12180
interfaceA{tag: "a",a};interfaceB{tag: "b",b};interfaceC{c};typeABC1=(A&C)|(B&C);lety : ABC1={tag:"a",a: "foo",c: "bar"}if(y.tag==="a")y.a// this is acceptedtypeABC2=(A|B)&C;letx : ABC2={tag:"a",a: "foo",c: "bar"}if(x.tag==="a")x.a// but this is currently an error :(
Expected behavior:
After checking x.tag==="a", the type of x should be narrowed to A & C Actual behavior:
There is no narrowing, so x.a results in a type error.
A workaround is given by ABC1, but in practice, this results in very long and redundant type definitions.
Activity
siegebell commentedon Nov 11, 2016
Hmm. It seems that
ABC1andABC2are not even considered equivalent types:Is this intended behavior? I.e. is there a case where
xandymight not be compatible?RyanCavanaugh commentedon Nov 11, 2016
This has been fixed by #11717
siegebell commentedon Nov 11, 2016
Ryan Cavanaugh (@RyanCavanaugh) phew -- Thanks!