@@ -5637,6 +5637,7 @@ namespace ts {
5637
5637
containsString?: boolean;
5638
5638
containsNumber?: boolean;
5639
5639
containsStringOrNumberLiteral?: boolean;
5640
+ unionIndex?: number;
5640
5641
}
5641
5642
5642
5643
function binarySearchTypes(types: Type[], type: Type): number {
@@ -5831,6 +5832,9 @@ namespace ts {
5831
5832
typeSet.containsAny = true;
5832
5833
}
5833
5834
else if (!(type.flags & TypeFlags.Never) && (strictNullChecks || !(type.flags & TypeFlags.Nullable)) && !contains(typeSet, type)) {
5835
+ if (type.flags & TypeFlags.Union && typeSet.unionIndex === undefined) {
5836
+ typeSet.unionIndex = typeSet.length;
5837
+ }
5834
5838
typeSet.push(type);
5835
5839
}
5836
5840
}
@@ -5857,15 +5861,6 @@ namespace ts {
5857
5861
if (types.length === 0) {
5858
5862
return emptyObjectType;
5859
5863
}
5860
- for (let i = 0; i < types.length; i++) {
5861
- const type = types[i];
5862
- if (type.flags & TypeFlags.Union) {
5863
- // We are attempting to construct a type of the form X & (A | B) & Y. Transform this into a type of
5864
- // the form X & A & Y | X & B & Y and recursively reduce until no union type constituents remain.
5865
- return getUnionType(map((<UnionType>type).types, t => getIntersectionType(replaceElement(types, i, t))),
5866
- /*subtypeReduction*/ false, aliasSymbol, aliasTypeArguments);
5867
- }
5868
- }
5869
5864
const typeSet = [] as TypeSet;
5870
5865
addTypesToIntersection(typeSet, types);
5871
5866
if (typeSet.containsAny) {
@@ -5874,6 +5869,14 @@ namespace ts {
5874
5869
if (typeSet.length === 1) {
5875
5870
return typeSet[0];
5876
5871
}
5872
+ const unionIndex = typeSet.unionIndex;
5873
+ if (unionIndex !== undefined) {
5874
+ // We are attempting to construct a type of the form X & (A | B) & Y. Transform this into a type of
5875
+ // the form X & A & Y | X & B & Y and recursively reduce until no union type constituents remain.
5876
+ const unionType = <UnionType>typeSet[unionIndex];
5877
+ return getUnionType(map(unionType.types, t => getIntersectionType(replaceElement(typeSet, unionIndex, t))),
5878
+ /*subtypeReduction*/ false, aliasSymbol, aliasTypeArguments);
5879
+ }
5877
5880
const id = getTypeListId(typeSet);
5878
5881
let type = intersectionTypes[id];
5879
5882
if (!type) {
0 commit comments