Normalize union/intersection type combinations - #11717
Merged
Anders Hejlsberg (ahejlsberg) merged 4 commits intoOct 19, 2016
Merged
Conversation
Nathan Shively-Sanders (sandersn)
requested changes
Oct 18, 2016
Nathan Shively-Sanders (sandersn)
left a comment
Member
There was a problem hiding this comment.
One question about isRelatedTo and a possible improvement for reporting errors with intersections of boolean.
| tests/cases/compiler/errorMessagesIntersectionTypes04.ts(18,5): error TS2322: Type 'A & B' is not assignable to type 'boolean'. | ||
| tests/cases/compiler/errorMessagesIntersectionTypes04.ts(19,5): error TS2322: Type 'A & B' is not assignable to type 'string'. | ||
| tests/cases/compiler/errorMessagesIntersectionTypes04.ts(21,5): error TS2322: Type 'number & boolean' is not assignable to type 'string'. | ||
| tests/cases/compiler/errorMessagesIntersectionTypes04.ts(21,5): error TS2322: Type '(number & true) | (number & false)' is not assignable to type 'string'. |
There was a problem hiding this comment.
Is there some way to present this error as number & boolean still? I feel like the distributed form is unexpected here.
With this PR we normalize combinations of intersection and union types based on the distributive property of the
&type operator over the|type operator. Specifically, becauseX & (A | B)is equivalent toX & A | X & B, we can transform intersection types with union type constituents into equivalent union types with intersection type constituents and thus ensure that union types are always at the top level in type representations and equivalent ways of writing the same type are treated identically.In the example above,
X1,X2, andX3are all identical types that reference the normalized formA & C | A & D | B & C | B & D.Fixes #9919.