diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index bab573c8b7f..4731046864e 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) || - getContainingFunctionOrSourceFile(declaration) !== getContainingFunctionOrSourceFile(node); + getContainingFunctionOrModule(declaration) !== getContainingFunctionOrModule(node); if (defaultsToDeclaredType && !(type.flags & TypeFlags.Narrowable)) { return type; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 2e51f6a1504..60b041d843c 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -840,11 +840,11 @@ namespace ts { } } - export function getContainingFunctionOrSourceFile(node: Node): FunctionLikeDeclaration | SourceFile { + export function getContainingFunctionOrModule(node: Node): Node { while (true) { node = node.parent; - if (isFunctionLike(node) || node.kind === SyntaxKind.SourceFile) { - return node; + if (isFunctionLike(node) || node.kind === SyntaxKind.ModuleDeclaration || node.kind === SyntaxKind.SourceFile) { + return node; } } }