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.
This commit is contained in:
Nathan Shively-Sanders 2016-12-07 13:14:00 -08:00
parent 7b1fc21460
commit 06b3ea9838

View File

@ -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;