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
Search Terms: Type union, type intersection, type inference
Code
interfaceA{a: boolean}interfaceA1extendsA{a1: boolean}interfaceA2extendsA{a2: boolean}typeA1andA2=A1&A2typeA1orA2=A1|A2typeA1orA2orA1andA2=A1orA2|A1andA2// Expectedconsttest1: A1orA2={a: true,a1: true,}console.log(test1)// Type inference: A1// Expectedconsttest2: A1orA2={a: true,a2: true,}console.log(test2)// Type inference: A2// Expectedconsttest3: A1orA2orA1andA2={a: true,a1: true,a2: true,}console.log(test3)// Type inference: A1andA2 <<< good// Expected => union accept the intersectionsconsttest4: A1orA2={a: true,a1: true,a2: true,}console.log(test4)// Type inference: A1 | A2 <<< Should be A1 & A2, not goodconsole.log(test4.a1)// Error console.log(test4.a2)// Error
It is not very clear in the documentation if the union contains the intersection, but both assumptions seems to have issues
Expected behavior:
Assumption: union contains the intersection A1 | A2 equals A1 or A2 or A1 & A2: the type intefence of { a: true, a1: true, a2: true } should be A1 & A2
Assumption: union excludes the intersection A1 | A2 equals A1 or A2 excluding A1 & A2: { a: true, a1: true, a2: true } should not be assignable to A1 | A2
Actual behavior:
Assumption: union contains the intersection
The type inference of { a: true, a1: true, a2: true } is A1 | A2 and we can not use the properties a1 and a2 without type guards
Assumption: union excludes the intersection { a: true, a1: true, a2: true } is still assignable to A1 | A2
Union includes the intersection, otherwise you would get weird behaviour such as 0 not being assignable to 0 | number. The operator behaves much the same as the set operation.
I wouldn't expect the inferred type of test4 to be A1 & A2 because of the structural nature of TypeScript. Partitioning the inferred type into A1 and A2 is arbitrary and it would be equally valid to infer {a: boolean; a1: boolean; a2; boolean}. You get this behaviour for test3 because you include it explicitly in the union type and it uses discrimination to remove A1orA2.
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.7.2
Search Terms: Type union, type intersection, type inference
Code
It is not very clear in the documentation if the union contains the intersection, but both assumptions seems to have issues
Expected behavior:
A1 | A2
equalsA1
orA2
orA1 & A2
: the type intefence of{ a: true, a1: true, a2: true }
should beA1 & A2
A1 | A2
equalsA1
orA2
excludingA1 & A2
:{ a: true, a1: true, a2: true }
should not be assignable toA1 | A2
Actual behavior:
The type inference of
{ a: true, a1: true, a2: true }
isA1 | A2
and we can not use the propertiesa1
anda2
without type guards{ a: true, a1: true, a2: true }
is still assignable toA1 | A2
Playground Link:
https://www.typescriptlang.org/play/index.html#src=interface%20A%20%7B%0D%0A%20%20%20%20a%3A%20boolean%0D%0A%7D%0D%0A%0D%0Ainterface%20A1%20extends%20A%20%7B%0D%0A%20%20%20%20a1%3A%20boolean%0D%0A%7D%0D%0A%0D%0Ainterface%20A2%20extends%20A%20%7B%0D%0A%20%20%20%20a2%3A%20boolean%0D%0A%7D%0D%0A%0D%0Atype%20A1andA2%20%3D%20A1%20%26%20A2%0D%0A%0D%0Atype%20A1orA2%20%3D%20A1%20%7C%20A2%0D%0A%0D%0Atype%20A1orA2orA1andA2%20%3D%20A1orA2%20%7C%20A1andA2%0D%0A%0D%0A%2F%2F%20Expected%0D%0Aconst%20test1%3A%20A1orA2%20%3D%20%7B%0D%0A%20%20%20%20a%3A%20true%2C%0D%0A%20%20%20%20a1%3A%20true%2C%0D%0A%7D%0D%0A%0D%0Aconsole.log(test1)%20%2F%2F%20Type%20inference%3A%20A1%0D%0A%0D%0A%2F%2F%20Expected%0D%0Aconst%20test2%3A%20A1orA2%20%3D%20%7B%0D%0A%20%20%20%20a%3A%20true%2C%0D%0A%20%20%20%20a2%3A%20true%2C%0D%0A%7D%0D%0A%0D%0Aconsole.log(test2)%20%2F%2F%20Type%20inference%3A%20A2%0D%0A%0D%0A%2F%2F%20Expected%0D%0Aconst%20test3%3A%20A1orA2orA1andA2%20%3D%20%7B%0D%0A%20%20%20%20a%3A%20true%2C%0D%0A%20%20%20%20a1%3A%20true%2C%0D%0A%20%20%20%20a2%3A%20true%2C%0D%0A%7D%0D%0A%0D%0Aconsole.log(test3)%20%2F%2F%20Type%20inference%3A%20A1andA2%20%3C%3C%3C%20good%0D%0A%0D%0A%2F%2F%20Expected%20%3D%3E%20union%20accept%20the%20intersections%0D%0Aconst%20test4%3A%20A1orA2%20%3D%20%7B%0D%0A%20%20%20%20a%3A%20true%2C%0D%0A%20%20%20%20a1%3A%20true%2C%0D%0A%20%20%20%20a2%3A%20true%2C%0D%0A%7D%0D%0A%0D%0Aconsole.log(test4)%20%2F%2F%20Type%20inference%3A%20A1%20%7C%20A2%20%3C%3C%3C%20Should%20be%20A1%20%26%20A2%2C%20not%20good%0D%0Aconsole.log(test4.a1)%20%2F%2F%20Error%20%0D%0Aconsole.log(test4.a2)%20%2F%2F%20Error
The text was updated successfully, but these errors were encountered: