Merge pull request #31572 from microsoft/normalizedIntersectionLimiter

Limit size of union types resulting from intersection type normalization
This commit is contained in:
Anders Hejlsberg
2019-05-23 17:57:33 -07:00
committed by GitHub
6 changed files with 542 additions and 0 deletions

View File

@@ -9912,6 +9912,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))),