From 0ff597c3a831ef39481714f3ad39c3b1e878aaaa Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 2 Jun 2018 17:42:07 -0700 Subject: [PATCH] Improve typeof x === "function" narrowing for non-unions --- src/compiler/checker.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index dc43faac435..9306907b418 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14112,11 +14112,14 @@ namespace ts { if (operator === SyntaxKind.ExclamationEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken) { assumeTrue = !assumeTrue; } + if (type.flags & TypeFlags.Any && literal.text === "function") { + return type; + } if (assumeTrue && !(type.flags & TypeFlags.Union)) { // We narrow a non-union type to an exact primitive type if the non-union type // is a supertype of that primitive type. For example, type 'any' can be narrowed // to one of the primitive types. - const targetType = typeofTypesByName.get(literal.text); + const targetType = literal.text === "function" ? globalFunctionType : typeofTypesByName.get(literal.text); if (targetType) { if (isTypeSubtypeOf(targetType, type)) { return targetType;