Skip to content

'instanceof' narrowing works awkwardly with empty object types & primitives #14426

Closed
@DanielRosenwasser

Description

@DanielRosenwasser

Try in the playground.

class Dog {
}

declare let x: Dog | string;

if (x instanceof Dog) {
    x
}
else {
    x.toLowerCase()
}

Expected: No error, first branch's x has type Dog, second branch's x has type string
Actual: Property 'toLowerCase' does not exist on type 'never'. (first branch is string | Dog, second is never)

We should be accounting for primitives a little less sloppily. instanceof checks don't work on primitive types.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions