From d735b7acbf790376d380a127f1de7950016b8b42 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 19 Apr 2016 10:04:17 -0700 Subject: [PATCH] Variables from different source files default to their declared type --- src/compiler/checker.ts | 2 +- src/compiler/utilities.ts | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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;