From c8bf6d821a5de7329ded94af1fa743068e85ace3 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 19 Apr 2016 10:14:02 -0700 Subject: [PATCH] Variables from different module declarations default to their declared type --- src/compiler/checker.ts | 2 +- src/compiler/utilities.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) 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; } } }