Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17668,9 +17668,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

function removeStringLiteralsMatchedByTemplateLiterals(types: Type[]) {
function removeStringLiteralsMatchedByTemplateLiterals(types: Type[]): Type[] | undefined {
const templates = filter(types, isPatternLiteralType) as (TemplateLiteralType | StringMappingType)[];
if (templates.length) {
const estimatedCount = templates.length * countWhere(types, t => !!(t.flags & TypeFlags.StringLiteral));
if (estimatedCount > 1000000) {
tracing?.instant(tracing.Phase.CheckTypes, "removeStringLiteralsMatchedByTemplateLiterals_DepthLimit", { typeIds: types.map(t => t.id) });
error(currentNode, Diagnostics.Expression_produces_a_union_type_that_is_too_complex_to_represent);
return undefined;
}

let i = types.length;
while (i > 0) {
i--;
Expand All @@ -17680,6 +17687,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
}
return types;
}

function isTypeMatchedByTemplateLiteralOrStringMapping(type: Type, template: TemplateLiteralType | StringMappingType) {
Expand Down Expand Up @@ -17804,7 +17812,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
removeRedundantLiteralTypes(typeSet, includes, !!(unionReduction & UnionReduction.Subtype));
}
if (includes & TypeFlags.StringLiteral && includes & (TypeFlags.TemplateLiteral | TypeFlags.StringMapping)) {
removeStringLiteralsMatchedByTemplateLiterals(typeSet);
typeSet = removeStringLiteralsMatchedByTemplateLiterals(typeSet);
if (!typeSet) {
return errorType;
}
}
if (includes & TypeFlags.IncludesConstrainedTypeVariable) {
removeConstrainedTypeVariables(typeSet);
Expand Down
5 changes: 4 additions & 1 deletion tests/baselines/reference/templateLiteralTypes1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ templateLiteralTypes1.ts(165,15): error TS1338: 'infer' declarations are only pe
templateLiteralTypes1.ts(197,16): error TS2590: Expression produces a union type that is too complex to represent.
templateLiteralTypes1.ts(201,16): error TS2590: Expression produces a union type that is too complex to represent.
templateLiteralTypes1.ts(205,16): error TS2590: Expression produces a union type that is too complex to represent.
templateLiteralTypes1.ts(251,7): error TS2590: Expression produces a union type that is too complex to represent.
templateLiteralTypes1.ts(252,7): error TS2590: Expression produces a union type that is too complex to represent.


==== templateLiteralTypes1.ts (7 errors) ====
==== templateLiteralTypes1.ts (8 errors) ====
// Template types example from #12754

const createScopedActionType = <S extends string>(scope: S) => <T extends string>(type: T) => `${scope}/${type}` as `${S}/${T}`;
Expand Down Expand Up @@ -276,6 +277,8 @@ templateLiteralTypes1.ts(252,7): error TS2590: Expression produces a union type
export type SpacingShorthand =
| `${Spacing} ${Spacing}`
| `${Spacing} ${Spacing} ${Spacing}`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2590: Expression produces a union type that is too complex to represent.
| `${Spacing} ${Spacing} ${Spacing} ${Spacing}`;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2590: Expression produces a union type that is too complex to represent.
Expand Down