Skip to content

Commit 0ff597c

Browse files
committed
Improve typeof x === "function" narrowing for non-unions
1 parent ed728f1 commit 0ff597c

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14112,11 +14112,14 @@ namespace ts {
1411214112
if (operator === SyntaxKind.ExclamationEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken) {
1411314113
assumeTrue = !assumeTrue;
1411414114
}
14115+
if (type.flags & TypeFlags.Any && literal.text === "function") {
14116+
return type;
14117+
}
1411514118
if (assumeTrue && !(type.flags & TypeFlags.Union)) {
1411614119
// We narrow a non-union type to an exact primitive type if the non-union type
1411714120
// is a supertype of that primitive type. For example, type 'any' can be narrowed
1411814121
// to one of the primitive types.
14119-
const targetType = typeofTypesByName.get(literal.text);
14122+
const targetType = literal.text === "function" ? globalFunctionType : typeofTypesByName.get(literal.text);
1412014123
if (targetType) {
1412114124
if (isTypeSubtypeOf(targetType, type)) {
1412214125
return targetType;

0 commit comments

Comments
 (0)