Merge pull request #1163 from Microsoft/typeGuards

Fixes to type guards
This commit is contained in:
Anders Hejlsberg
2014-11-14 15:36:07 -08:00
19 changed files with 510 additions and 391 deletions

View File

@@ -4319,13 +4319,9 @@ module ts {
var type = getTypeOfSymbol(symbol);
// Only narrow when symbol is variable of a structured type
if (node && (symbol.flags & SymbolFlags.Variable && type.flags & TypeFlags.Structured)) {
while (true) {
loop: while (true) {
var child = node;
node = node.parent;
// Stop at containing function or module block
if (!node || node.kind === SyntaxKind.FunctionBlock || node.kind === SyntaxKind.ModuleBlock) {
break;
}
var narrowedType = type;
switch (node.kind) {
case SyntaxKind.IfStatement:
@@ -4351,9 +4347,18 @@ module ts {
}
}
break;
case SyntaxKind.SourceFile:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.Method:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.Constructor:
// Stop at the first containing function or module declaration
break loop;
}
// Only use narrowed type if construct contains no assignments to variable
if (narrowedType !== type) {
// Use narrowed type if it is a subtype and construct contains no assignments to variable
if (narrowedType !== type && isTypeSubtypeOf(narrowedType, type)) {
if (isVariableAssignedWithin(symbol, node)) {
break;
}