mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-10 18:04:18 -05:00
Divide-and-conquer strategy for intersections of unions (#57871)
This commit is contained in:
@@ -17763,6 +17763,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
removeFromEach(typeSet, TypeFlags.Null);
|
||||
result = getUnionType([getIntersectionType(typeSet), nullType], UnionReduction.Literal, aliasSymbol, aliasTypeArguments);
|
||||
}
|
||||
else if (typeSet.length >= 4) {
|
||||
// When we have four or more constituents, some of which are unions, we employ a "divide and conquer" strategy
|
||||
// where A & B & C & D is processed as (A & B) & (C & D). Since intersections of unions often produce far smaller
|
||||
// unions of intersections than the full cartesian product (due to some intersections becoming `never`), this can
|
||||
// dramatically reduce the overall work.
|
||||
const middle = Math.floor(typeSet.length / 2);
|
||||
result = getIntersectionType([getIntersectionType(typeSet.slice(0, middle)), getIntersectionType(typeSet.slice(middle))], aliasSymbol, aliasTypeArguments);
|
||||
}
|
||||
else {
|
||||
// We are attempting to construct a type of the form X & (A | B) & (C | D). Transform this into a type of
|
||||
// the form X & A & C | X & A & D | X & B & C | X & B & D. If the estimated size of the resulting union type
|
||||
|
||||
Reference in New Issue
Block a user