From 06b3ea98386a3650faf96f196c2081ae40e43bf7 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Wed, 7 Dec 2016 13:14:00 -0800 Subject: [PATCH] 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. --- src/compiler/checker.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cdef31ee400..650279ad52f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14380,14 +14380,18 @@ namespace ts { } // TypeScript 1.0 spec (April 2014): 4.15.4 // The instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type, - // and the right operand to be of type Any or a subtype of the 'Function' interface type. + // and the right operand to be of type Any, a subtype of the 'Function' interface type, or have a call or construct signature. // The result is always of the Boolean primitive type. // NOTE: do not raise error if leftType is unknown as related error was already reported if (isTypeOfKind(leftType, TypeFlags.Primitive)) { error(left, Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter); } // NOTE: do not raise error if right is unknown as related error was already reported - if (!(isTypeAny(rightType) || isTypeSubtypeOf(rightType, globalFunctionType))) { + if (!(isTypeAny(rightType) || + rightType.flags & TypeFlags.Nullable || + getSignaturesOfType(rightType, SignatureKind.Call).length || + getSignaturesOfType(rightType, SignatureKind.Construct).length || + isTypeSubtypeOf(rightType, globalFunctionType))) { 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); } return booleanType;