Skip to content

Commit 06b3ea9

Browse files
committed
instanceof RHS must be any, callable/constructable or Function subtype
Also includes a fast-path for null and undefined even though they are technically Function subtypes.
1 parent 7b1fc21 commit 06b3ea9

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14380,14 +14380,18 @@ namespace ts {
1438014380
}
1438114381
// TypeScript 1.0 spec (April 2014): 4.15.4
1438214382
// The instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type,
14383-
// and the right operand to be of type Any or a subtype of the 'Function' interface type.
14383+
// and the right operand to be of type Any, a subtype of the 'Function' interface type, or have a call or construct signature.
1438414384
// The result is always of the Boolean primitive type.
1438514385
// NOTE: do not raise error if leftType is unknown as related error was already reported
1438614386
if (isTypeOfKind(leftType, TypeFlags.Primitive)) {
1438714387
error(left, Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
1438814388
}
1438914389
// NOTE: do not raise error if right is unknown as related error was already reported
14390-
if (!(isTypeAny(rightType) || isTypeSubtypeOf(rightType, globalFunctionType))) {
14390+
if (!(isTypeAny(rightType) ||
14391+
rightType.flags & TypeFlags.Nullable ||
14392+
getSignaturesOfType(rightType, SignatureKind.Call).length ||
14393+
getSignaturesOfType(rightType, SignatureKind.Construct).length ||
14394+
isTypeSubtypeOf(rightType, globalFunctionType))) {
1439114395
error(right, Diagnostics.The_right_hand_side_of_an_instanceof_expression_must_be_of_type_any_or_of_a_type_assignable_to_the_Function_interface_type);
1439214396
}
1439314397
return booleanType;

0 commit comments

Comments
 (0)