Use the existing checkCrossProductUnion helper

This commit is contained in:
Andrew Casey 2020-09-25 13:37:54 -07:00
parent 6650496e85
commit 9f5310fd8d
3 changed files with 9 additions and 19 deletions

View File

@ -13394,6 +13394,7 @@ namespace ts {
function checkCrossProductUnion(types: readonly Type[]) {
const size = reduceLeft(types, (n, t) => n * (t.flags & TypeFlags.Union ? (<UnionType>t).types.length : t.flags & TypeFlags.Never ? 0 : 1), 1);
if (size >= 100000) {
tracing.instant(tracing.Phase.Check, "checkCrossProductUnion_DepthLimit", { typeIds: types.map(t => t.id), size });
error(currentNode, Diagnostics.Expression_produces_a_union_type_that_is_too_complex_to_represent);
return false;
}
@ -14458,14 +14459,18 @@ namespace ts {
if (merged) {
return getSpreadType(merged, right, symbol, objectFlags, readonly);
}
return errorTypeIfTooLarge() ?? mapType(left, t => getSpreadType(t, right, symbol, objectFlags, readonly));
return checkCrossProductUnion([left, right])
? mapType(left, t => getSpreadType(t, right, symbol, objectFlags, readonly))
: errorType;
}
if (right.flags & TypeFlags.Union) {
const merged = tryMergeUnionOfObjectTypeAndEmptyObject(right as UnionType, readonly);
if (merged) {
return getSpreadType(left, merged, symbol, objectFlags, readonly);
}
return errorTypeIfTooLarge() ?? mapType(right, t => getSpreadType(left, t, symbol, objectFlags, readonly));
return checkCrossProductUnion([left, right])
? mapType(right, t => getSpreadType(left, t, symbol, objectFlags, readonly))
: errorType;
}
if (right.flags & (TypeFlags.BooleanLike | TypeFlags.NumberLike | TypeFlags.BigIntLike | TypeFlags.StringLike | TypeFlags.EnumLike | TypeFlags.NonPrimitive | TypeFlags.Index)) {
return left;
@ -14544,17 +14549,6 @@ namespace ts {
getIndexInfoWithReadonly(numberIndexInfo, readonly));
spread.objectFlags |= ObjectFlags.ObjectLiteral | ObjectFlags.ContainsObjectOrArrayLiteral | ObjectFlags.ContainsSpread | objectFlags;
return spread;
function errorTypeIfTooLarge(): Type | undefined {
if (left.flags & right.flags & TypeFlags.Union) {
const resultSize = (left as UnionType).types.length * (right as UnionType).types.length;
if (resultSize > 100000) {
tracing.instant(tracing.Phase.Check, "getSpreadType_DepthLimit", { leftId: left.id, rightId: right.id });
error(currentNode, Diagnostics.Spread_expression_produces_a_union_type_that_is_too_complex_to_represent);
return errorType;
}
}
}
}
/** We approximate own properties as non-methods plus methods that are inside the object literal */

View File

@ -3048,10 +3048,6 @@
"category": "Error",
"code": 2795
},
"Spread expression produces a union type that is too complex to represent.": {
"category": "Error",
"code": 2796
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",

View File

@ -1,4 +1,4 @@
tests/cases/conformance/types/spread/objectSpreadRepeatedComplexity.ts(3,12): error TS2796: Spread expression produces a union type that is too complex to represent.
tests/cases/conformance/types/spread/objectSpreadRepeatedComplexity.ts(3,12): error TS2590: Expression produces a union type that is too complex to represent.
==== tests/cases/conformance/types/spread/objectSpreadRepeatedComplexity.ts (1 errors) ====
@ -166,5 +166,5 @@ tests/cases/conformance/types/spread/objectSpreadRepeatedComplexity.ts(3,12): er
~~~~~~~~~~~
};
~~~~~
!!! error TS2796: Spread expression produces a union type that is too complex to represent.
!!! error TS2590: Expression produces a union type that is too complex to represent.
}