From ed40fbf2d8fbdca5a6407d5ce07df832094643ca Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 19 Feb 2016 16:48:58 -0800 Subject: [PATCH] Suport both x != null and x != undefined in non-null type guards --- src/compiler/checker.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 874dcb40d5a..eafbff70822 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6986,6 +6986,11 @@ namespace ts { } } + function isNullOrUndefinedLiteral(node: Expression) { + return node.kind === SyntaxKind.NullKeyword || + node.kind === SyntaxKind.Identifier && getResolvedSymbol(node) === undefinedSymbol; + } + // Get the narrowed type of a given symbol at a given location function getNarrowedTypeOfSymbol(symbol: Symbol, node: Node) { let type = getTypeOfSymbol(symbol); @@ -7069,7 +7074,7 @@ namespace ts { switch (expr.operatorToken.kind) { case SyntaxKind.EqualsEqualsToken: case SyntaxKind.ExclamationEqualsToken: - if (expr.right.kind === SyntaxKind.NullKeyword) { + if (isNullOrUndefinedLiteral(expr.right)) { return narrowTypeByNullCheck(type, expr, assumeTrue); } // Fall through