diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e6d3ec5ccea..44713b2c1db 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6444,8 +6444,10 @@ namespace ts { function inferTypes(context: InferenceContext, source: Type, target: Type) { let sourceStack: Type[]; let targetStack: Type[]; + const maxDepth = 5; let depth = 0; let inferiority = 0; + const visited: Map = {}; inferFromTypes(source, target); function isInProcess(source: Type, target: Type) { @@ -6571,10 +6573,21 @@ namespace ts { if (isInProcess(source, target)) { return; } + // we delibirately limit the depth we examine to infer types: this speeds up the overall inference process + // and user rarely expects inferences to be made from the deeply nested constituents. + if (depth > maxDepth) { + return; + } if (isDeeplyNestedGeneric(source, sourceStack, depth) && isDeeplyNestedGeneric(target, targetStack, depth)) { return; } + const key = source.id + "," + target.id; + if (hasProperty(visited, key)) { + return; + } + visited[key] = true; + if (depth === 0) { sourceStack = []; targetStack = [];