Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Milestone
Description
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 TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Activity
ahejlsberg commentedon Nov 19, 2017
This was an error in previous releases too because
Cis an empty object type. However, with #19671 we can (and should) get this right.abramobagnara commentedon Nov 19, 2017
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 commentedon 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.