Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instanceof type guard missing deduction #20138

Closed
abramobagnara opened this issue Nov 19, 2017 · 3 comments
Closed

instanceof type guard missing deduction #20138

abramobagnara opened this issue Nov 19, 2017 · 3 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@abramobagnara
Copy link

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'.

@ahejlsberg
Copy link
Member

This was an error in previous releases too because C is an empty object type. However, with #19671 we can (and should) get this right.

@ahejlsberg ahejlsberg self-assigned this Nov 19, 2017
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Nov 19, 2017
@abramobagnara
Copy link
Author

From last part of comment above it seems you imply that master branch after #19671 merge get this right, but this is not the case. Am I missing something?

I can confirm that if C is not an empty object type Typescript 2.6.1 compile the source without errors.

@ahejlsberg
Copy link
Member

ahejlsberg commented Nov 19, 2017

@abramobagnara I meant that with the approach we introduced in #19671 we can, but currently don't, get it right even when C is an empty class type (as in your example). You are correct that it used to work when C is not an empty class. Both issues are fixed by #20142 that I just put up.

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Nov 19, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

2 participants