Skip to content

InstanceOf incorrectly narrow when two type extends same base class #10422

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

Closed
yuit opened this issue Aug 18, 2016 · 5 comments
Closed

InstanceOf incorrectly narrow when two type extends same base class #10422

yuit opened this issue Aug 18, 2016 · 5 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@yuit
Copy link
Contributor

yuit commented Aug 18, 2016

Version: master

class B { name: string }
class A { }
class C { name: string; }
var a: A | C;
if (!(a instanceof A)) {
    a;  // a has "never" type 
}

The change is a result of the PR #10216. It is followed the specification in that PR. Though I wonder whether this should get treated differently.

@yuit yuit added the Bug A bug in TypeScript label Aug 18, 2016
@yortus
Copy link
Contributor

yortus commented Aug 19, 2016

You can remove extends B from both the A and C classes and see the same behaviour, so extending the same base class doesn't seem important here.

Isn't this because C is structurally a subtype of A? So when the type guard removes A, it also removes C which it considers a subtype, leaving never as the narrowed type.

The PR #10216 addressed a very similar situation raised in #7271, but the problem and the fix addressed the situation where unrelated classes were structurally identical. This is a case where unrelated classes have a structural subtype-supertype relationship.

You can see the same behaviour with the first example in #7271 (comment).

@yuit
Copy link
Contributor Author

yuit commented Aug 19, 2016

@yortus thanks for correcting my example! Yes, it is because C is subtype of A but structurally not identical to A which according to PR #10216, then type C is instance of type A.

This example is slight different from the example in your comment comment in that we will treat user-defined typeguard and instanceof slightly different as @mhegazy pointed out here

@yuit yuit changed the title InstanceOf incorrectly narrow when two type extends same base calss InstanceOf incorrectly narrow when two type extends same base class Aug 19, 2016
@yortus
Copy link
Contributor

yortus commented Aug 20, 2016

@yuit this behaviour was the same before #10216 was merged - you can see it for example using [email protected]. #10216 tweaked the existing narrowing behaviour for structurally identical types, but I don't think it had any effect on this scenario.

@mhegazy mhegazy added Design Limitation Constraints of the existing architecture prevent this from being fixed and removed Bug A bug in TypeScript labels Dec 6, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Dec 6, 2016

That is a current design limitation with structural comparison.

@ericmorand
Copy link

So when will this bug be fixed?

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

4 participants