| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
There was a problem hiding this comment.
One question about isRelatedTo and a possible improvement for reporting errors with intersections of boolean.
Sorry, something went wrong.
| 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.
Sorry, something went wrong.
There was a problem hiding this comment.
The actual representation needs to be (number & true) | (number & false), but we could play tricks in the type-to-string logic to turn it into number & boolean. Not sure it's worth it though as those types are all rather meaningless anyway.
Sorry, something went wrong.
There was a problem hiding this comment.
Yeah, type-to-string is what I was thinking of. But you're right, & boolean probably only shows up in our tests.
Sorry, something went wrong.
| return result; | ||
| } | ||
| } | ||
| else if (source.flags & TypeFlags.Intersection) { |
There was a problem hiding this comment.
why not check the source first here too? I think that's the normal pattern for isRelatedTo
Sorry, something went wrong.
There was a problem hiding this comment.
The comment above the initial if statement explains why. For intersection types, the target side is an "each" relationship and the source side is a "some" relationship. We need to deconstruct "each" relationships first to get the correct results.
Sorry, something went wrong.
There was a problem hiding this comment.
OK, so I think I misunderstood the parts of the code that are independent post-distribution. It would work to put intersections before unions now, where it wouldn't before, right?
Sorry, something went wrong.
| return result; | ||
| } | ||
| } | ||
| else if (source.flags & TypeFlags.Intersection) { |
There was a problem hiding this comment.
OK, so I think I misunderstood the parts of the code that are independent post-distribution. It would work to put intersections before unions now, where it wouldn't before, right?
Sorry, something went wrong.
| 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.
Yeah, type-to-string is what I was thinking of. But you're right, & boolean probably only shows up in our tests.
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
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, because X & (A | B) is equivalent to X & 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, and X3 are all identical types that reference the normalized form A & C | A & D | B & C | B & D.
Fixes #9919.