Skip to content

Commit 027829f

Browse files
committed
Properly handle edge cases
1 parent 8b6e853 commit 027829f

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/compiler/checker.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -8457,9 +8457,11 @@ namespace ts {
84578457
}
84588458
i--;
84598459
}
8460-
if (intersection !== unionType.types) {
8461-
types[unionIndex] = getUnionTypeFromSortedList(intersection, unionType.flags & TypeFlags.UnionOfUnitTypes);
8460+
if (intersection === unionType.types) {
8461+
return false;
84628462
}
8463+
types[unionIndex] = getUnionTypeFromSortedList(intersection, unionType.flags & TypeFlags.UnionOfUnitTypes);
8464+
return true;
84638465
}
84648466

84658467
// We normalize combinations of intersection and union types based on the distributive property of the '&'
@@ -8489,16 +8491,19 @@ namespace ts {
84898491
includes & TypeFlags.ESSymbol && includes & TypeFlags.UniqueESSymbol) {
84908492
removeRedundantPrimitiveTypes(typeSet, includes);
84918493
}
8492-
if (includes & TypeFlags.UnionOfUnitTypes) {
8493-
intersectUnionsOfUnitTypes(typeSet);
8494-
}
84958494
if (includes & TypeFlags.EmptyObject && !(includes & TypeFlags.Object)) {
84968495
typeSet.push(emptyObjectType);
84978496
}
84988497
if (typeSet.length === 1) {
84998498
return typeSet[0];
85008499
}
85018500
if (includes & TypeFlags.Union) {
8501+
if (includes & TypeFlags.UnionOfUnitTypes && intersectUnionsOfUnitTypes(typeSet)) {
8502+
// When the intersection creates a reduced set (which might mean that *all* union types have
8503+
// disappeared), we restart the operation to get a new set of combined flags. Once we have
8504+
// reduced we'll never reduce again, so this occurs at most once.
8505+
return getIntersectionType(typeSet, aliasSymbol, aliasTypeArguments);
8506+
}
85028507
// We are attempting to construct a type of the form X & (A | B) & Y. Transform this into a type of
85038508
// the form X & A & Y | X & B & Y and recursively reduce until no union type constituents remain.
85048509
const unionIndex = findIndex(typeSet, t => (t.flags & TypeFlags.Union) !== 0);

0 commit comments

Comments
 (0)