Skip to content

instanceof does not narrow when the class is intersected with &Β #50844

Closed
@ehmicky

Description

@ehmicky

πŸ”Ž 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 to 4.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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions