diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 615d6f1834c..2eaf91b9614 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6386,6 +6386,8 @@ namespace ts { // Only narrow when symbol is variable of type any or an object, union, or type parameter type if (node && symbol.flags & SymbolFlags.Variable) { if (isTypeAny(type) || type.flags & (TypeFlags.ObjectType | TypeFlags.Union | TypeFlags.TypeParameter)) { + const declaration = getDeclarationOfKind(symbol, SyntaxKind.VariableDeclaration); + const top = declaration && getDeclarationContainer(declaration); const originalType = type; const nodeStack: {node: Node, child: Node}[] = []; loop: while (node.parent) { @@ -6402,6 +6404,9 @@ namespace ts { // Stop at the first containing file or module declaration break loop; } + if (node === top) { + break; + } } let nodes: {node: Node, child: Node};