From f057d4efd80c2d417de937256fb22bba413e61ad Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Sun, 30 Nov 2014 19:23:06 -0800 Subject: [PATCH] CR feedback. --- src/compiler/checker.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ef234ac6c4b..1725d2f9b7c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4420,19 +4420,25 @@ module ts { return type; function narrowTypeByEquality(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type { - var left = expr.left; - var right = expr.right; // Check that we have 'typeof ' on the left and string literal on the right - if (left.kind !== SyntaxKind.TypeOfExpression || - left.expression.kind !== SyntaxKind.Identifier || right.kind !== SyntaxKind.StringLiteral || - getResolvedSymbol(left.expression) !== symbol) { + if (expr.left.kind !== SyntaxKind.TypeOfExpression || expr.right.kind !== SyntaxKind.StringLiteral) { return type; } + + var left = expr.left; + var right = expr.right; + if (left.expression.kind !== SyntaxKind.Identifier || + getResolvedSymbol(left.expression) !== symbol) { + + return type; + } + var t = right.text; var checkType: Type = t === "string" ? stringType : t === "number" ? numberType : t === "boolean" ? booleanType : emptyObjectType; if (expr.operator === SyntaxKind.ExclamationEqualsEqualsToken) { assumeTrue = !assumeTrue; } + if (assumeTrue) { // The assumed result is true. If check was for a primitive type, that type is the narrowed type. Otherwise we can // remove the primitive types from the narrowed type.