Skip to content

Commit f20a4fd

Browse files
committed
Limit size of union types resulting from intersection type normalization
1 parent e601333 commit f20a4fd

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9908,6 +9908,12 @@ namespace ts {
99089908
else {
99099909
// We are attempting to construct a type of the form X & (A | B) & Y. Transform this into a type of
99109910
// the form X & A & Y | X & B & Y and recursively reduce until no union type constituents remain.
9911+
// If the estimated size of the resulting union type exceeds 100000 constituents, report an error.
9912+
const size = reduceLeft(typeSet, (n, t) => n * (t.flags & TypeFlags.Union ? (<UnionType>t).types.length : 1), 1);
9913+
if (size >= 100000) {
9914+
error(currentNode, Diagnostics.Expression_produces_a_union_type_that_is_too_complex_to_represent);
9915+
return errorType;
9916+
}
99119917
const unionIndex = findIndex(typeSet, t => (t.flags & TypeFlags.Union) !== 0);
99129918
const unionType = <UnionType>typeSet[unionIndex];
99139919
result = getUnionType(map(unionType.types, t => getIntersectionType(replaceElement(typeSet, unionIndex, t))),

0 commit comments

Comments
 (0)