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

Type Guard on Field does not Narrow Type of Object #25447

Closed
AnyhowStep opened this issue Jul 5, 2018 · 1 comment
Closed

Type Guard on Field does not Narrow Type of Object #25447

AnyhowStep opened this issue Jul 5, 2018 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@AnyhowStep
Copy link
Contributor

TypeScript Version: 3.0.0-dev.20180602

Search Terms: field type guard narrow

Code

interface A {
  f?: number;
  bar: string; 
}

interface B {
  f: number; 
}
declare const x: A | B;
if (x.f == undefined) {
  //Expected:
  //'x' is narrowed to type 'A'
  //Actual:
  //Property 'bar' does not exist on type 'A | B'.
  //Property 'bar' does not exist on type 'B'.
  console.log(x.bar);
} 

Expected behavior:

'x' is narrowed to type 'A'

Actual behavior:

Property 'bar' does not exist on type 'A | B'.
Property 'bar' does not exist on type 'B'.

Playground Link: Here

@mhegazy
Copy link
Contributor

mhegazy commented Jul 5, 2018

This is in the vein of #24193, with undefined instead of null. I would recommend adding this use case to #24193

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jul 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants