Skip to content

instanceof type guard missing deduction #20138

Closed
@abramobagnara

Description

@abramobagnara

TypeScript Version: 2.7.0-dev.201xxxxx

Code

class C { }

function f<T extends C>(v: T | string): void {
  if (v instanceof C) {
    const x: T = v;
    console.log(x);
  }
}

Expected behavior:

No compile errors:

Actual behavior:

p.ts(5,11): error TS2322: Type 'string | T' is not assignable to type 'T'.
Type 'string' is not assignable to type 'T'.

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