Skip to content

Commit

Permalink
Limit size of union types resulting from intersection type normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
ahejlsberg committed May 23, 2019
1 parent e601333 commit f20a4fd
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/compiler/checker.ts
Expand Up @@ -9908,6 +9908,12 @@ namespace ts {
else {
// We are attempting to construct a type of the form X & (A | B) & Y. Transform this into a type of
// the form X & A & Y | X & B & Y and recursively reduce until no union type constituents remain.
// If the estimated size of the resulting union type exceeds 100000 constituents, report an error.
const size = reduceLeft(typeSet, (n, t) => n * (t.flags & TypeFlags.Union ? (<UnionType>t).types.length : 1), 1);
if (size >= 100000) {
error(currentNode, Diagnostics.Expression_produces_a_union_type_that_is_too_complex_to_represent);
return errorType;
}
const unionIndex = findIndex(typeSet, t => (t.flags & TypeFlags.Union) !== 0);
const unionType = <UnionType>typeSet[unionIndex];
result = getUnionType(map(unionType.types, t => getIntersectionType(replaceElement(typeSet, unionIndex, t))),
Expand Down

0 comments on commit f20a4fd

Please sign in to comment.