Closed
Description
π Search Terms
"instanceof", "Symbol.hasInstance"
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about instanceof
β― Playground Link
π» Code
const EvenNumbers = {
[Symbol.hasInstance]: v => typeof v === 'number' && v % 2 == 0
};
if (3 instanceof EvenNumbers) {
console.log('mhh what?')
}
if (10 instanceof EvenNumbers) {
console.log("10 is even")
}
π Actual behavior
Typescript shows an error on the 3
and the 10
:
The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter. (2358)
π Expected behavior
It should not show any error, because the code is valid JS
Additional information about the issue
After having support for hasInstance
#55052, this actually means that the left-hand side operand doesn't have to be an object anymore. It can perfectly be primitives, as shown by this example (albeit contrieved...)
The spec for relational expressions also allows any kind of relational expressions (which also includes anything that resolves in a primitive) as well https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-relational-operators