diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 398724fca93..1830eafd1f4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13394,6 +13394,7 @@ namespace ts { function checkCrossProductUnion(types: readonly Type[]) { const size = reduceLeft(types, (n, t) => n * (t.flags & TypeFlags.Union ? (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 */ diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index c7255c40ba4..f2ea2fc68d6 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -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", diff --git a/tests/baselines/reference/objectSpreadRepeatedComplexity.errors.txt b/tests/baselines/reference/objectSpreadRepeatedComplexity.errors.txt index e9ad041f28d..d411c319605 100644 --- a/tests/baselines/reference/objectSpreadRepeatedComplexity.errors.txt +++ b/tests/baselines/reference/objectSpreadRepeatedComplexity.errors.txt @@ -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. } \ No newline at end of file