Closed
Description
Bug Report
π Search Terms
insatnceof extends function
π Version & Regression Information
- This changed between versions 5.0.0-dev.20221129 and 5.0.0-dev.20221130
β― Playground Link
Playground link with relevant code
π» Code
class PersonMixin extends Function {
public [Symbol.hasInstance](o: any) {
return typeof o === "object" && o !== null && o instanceof Person;
}
}
const cls = new PersonMixin();
class Person {
work(): void { console.log("work") }
sayHi(): void { console.log("Hi") }
}
class Car {
sayHi(): void { console.log("Wof Wof") }
}
function test(o: Person | Car) {
if (o instanceof cls) {
console.log("Is Person");
(o as Person).work()
}
else {
console.log("Is Car")
o.sayHi(); //o is never in 5.0. Was Person | Car in 4.9
}
}
test(new Person)
test(new Car)
π Actual behavior
o
is never
on the else branch. causing an error
π Expected behavior
o
is Person | Car
as it was in 4.9 since the type guard doesn't really say a lot about the type of o
in the type system.
Activity
ahejlsberg commentedon Feb 3, 2023
This is an effect of #51631. It fixed
isTypeDerivedFrom
to correctly handle{}
, but turns outnarrowTypeByInstanceof
was relying on the old inconsistent behavior. That needs fixing too.narrowTypeByInstanceof
#52592