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

Nested optional property existence check via Boolean() cause false possibly undefined error #11088

Closed
zfeher opened this issue Sep 23, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@zfeher
Copy link

zfeher commented Sep 23, 2016

TypeScript Version: 2.0.3

Code
Nested optional property existence check via !! vs. Boolean()

// strictNullChecks: true

interface Address {
  country: string;
}

interface Student {
  address?: Address;
}

const student: Student = {
  address: {
    country: 'US',
  }
};

// this one works just fine, no error reported
const b1: boolean = student && !!student.address && student.address.country === 'US';

// this one reports an error for the second student.address: Object is possibly 'undefined' 
// whereas !!something should be the same as Boolean(something)
const b2: boolean = student && Boolean(student.address) && student.address.country === 'US';

Expected behavior:
"Object is possibly 'undefined' error" should not be reported when accessing student.address after the existence of address on student is verified.

Actual behavior:
"Object is possibly 'undefined' error" is reported when student.address is accessed the second time after its existence is already verified.

@kitsonk
Copy link
Contributor

kitsonk commented Sep 23, 2016

Related to/dupe of #9508 (in the sense of expanding guards that are recognised by CFA)

@mhegazy mhegazy closed this as completed Sep 23, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label Sep 23, 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