Remove empty intersection types in union types#18438
Merged
ahejlsberg merged 6 commits intomasterfrom Sep 14, 2017
Merged
Conversation
weswigham
reviewed
Sep 13, 2017
src/compiler/checker.ts
Outdated
| if (!(flags & TypeFlags.ContainsWideningType)) typeSet.containsNonWideningType = true; | ||
| } | ||
| else if (!(flags & TypeFlags.Never)) { | ||
| else if (!(flags & TypeFlags.Never || flags & TypeFlags.Intersection && every((<IntersectionType>type).types, isUnitType))) { |
Member
There was a problem hiding this comment.
Rather than checking if every type is unit, is it not sufficient to check if any two members are unit? number & 3 & 4 is as vacuous as 3 & 4, right?
mhegazy
approved these changes
Sep 14, 2017
|
what about tag types #4895? type CustomerId = number & 'customer-id'; |
Contributor
|
Use |
|
Is |
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An intersection of distinct unit types (such as
'a' & 'b' & 'c') is effectively the same asneverbecause it has en empty set of possible values. We allow such types to exist primarily to make it easier to discover their origin (e.g. an intersection of object types containing two properties with the same name). However, when intersecting unions of unit types, the normalization introduced by #11717 ends up making the types very noisy. For example(0 | 1 | 2) & (1 | 2 | 3)becomes1 | 2 | (0 & 1) | (0 & 2) | (0 & 3) | (1 & 2) | (1 & 3) | (2 & 1) | (2 & 3).Likewise, when intersecting
{ a: string } | undefinedand{ b: number } | undefined(similar to #18210), normalization produces a union of{ a: string } & { b: number },{ a: string } & undefined,undefined & { b: number }andundefined. The middle two have empty sets of values, but trip up property accesses through values of the union type.With this PR we now remove an intersection type from a union type if
nullorundefined).Some examples:
We could in theory be more aggressive about removing empty intersection types, but we don't want to break code that uses intersections to "tag" primitive types.
This implements parts of what is suggested in #16386 and fixes #18210.