diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d58ad9fbf9a..bab573c8b7f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7936,7 +7936,7 @@ namespace ts { const declaration = localOrExportSymbol.valueDeclaration; const defaultsToDeclaredType = !strictNullChecks || !declaration || declaration.kind === SyntaxKind.Parameter || isInAmbientContext(declaration) || - getContainingFunction(declaration) !== getContainingFunction(node); + getContainingFunctionOrSourceFile(declaration) !== getContainingFunctionOrSourceFile(node); if (defaultsToDeclaredType && !(type.flags & TypeFlags.Narrowable)) { return type; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 638c7a60aa1..2e51f6a1504 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -840,6 +840,15 @@ namespace ts { } } + export function getContainingFunctionOrSourceFile(node: Node): FunctionLikeDeclaration | SourceFile { + while (true) { + node = node.parent; + if (isFunctionLike(node) || node.kind === SyntaxKind.SourceFile) { + return node; + } + } + } + export function getContainingClass(node: Node): ClassLikeDeclaration { while (true) { node = node.parent;