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

Improved union/intersection type inference bug? #7603

Closed
bartosz-k opened this issue Mar 20, 2016 · 2 comments
Closed

Improved union/intersection type inference bug? #7603

bartosz-k opened this issue Mar 20, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@bartosz-k
Copy link

Just read about this new TypeScript feature and tried to use it in my project, but...

TypeScript Version:

1.8.9

Code

type T = {a:any} | {b:any};

let myObject:{c:T};

function hasA(value:T):value is {a:any}{
  return 'a' in value;
}
if(hasA(myObject.c)){
  myObject.c.a; // <- the error goes from here
}

Expected behavior:

No error.

Actual behavior:

Error:(24, 14) TS2339: Property 'a' does not exist on type '{ a: any; } | { b: any; }'.

Shouldn't it be considered as a bug?

@yortus
Copy link
Contributor

yortus commented Mar 21, 2016

Duplicate of #3812?

That is, the problem is that type guards don't yet work with property accesses (like myObject.c). Your example works if the property access is done before the type guard, which can then use a simple variable:

let c = myObject.c;
if(hasA(c)){
  c.a; // <- no error here anymore
}

@vladima vladima added the Duplicate An existing issue was already created label Mar 21, 2016
@vladima
Copy link
Contributor

vladima commented Mar 21, 2016

@yortus is correct, also #7140 should also address this issue, see section Dotted names in type guards

@vladima vladima closed this as completed Mar 21, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants