Closed
Description
π Search Terms
instanceof
, narrow
.
π Version & Regression Information
- This is a crash
- This is the behavior in every version I tried (from
3.8.3
to to4.9.0-dev.20220919
), and I reviewed the FAQ for entries about it.
β― Playground Link
Playground link with relevant code
π» Code
interface InstanceOne {
one(): void
}
interface InstanceTwo {
two(): void
}
const instance = {} as InstanceOne | InstanceTwo
const ClassOneWrong = {} as { new (): InstanceOne } & { foo: true }
if (instance instanceof ClassOneWrong) {
instance.one() // Property 'one' does not exist, because `instance` is not narrowed down
}
const ClassOneRight = {} as { new (): InstanceOne, foo: true }
if (instance instanceof ClassOneRight) {
instance.one() // Property 'one' exists and `instance` is narrowed down
}
π Actual behavior
instanceof
does not narrow down the left side when the right side is a type using &
.
π Expected behavior
It should narrow it down to InstanceOne
, and instance.one()
should pass.