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

Code Flow analysis truthy checks not working for literal booleans #12939

Closed
basarat opened this issue Dec 15, 2016 · 3 comments
Closed

Code Flow analysis truthy checks not working for literal booleans #12939

basarat opened this issue Dec 15, 2016 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@basarat
Copy link
Contributor

basarat commented Dec 15, 2016

TypeScript Version: nightly (2.2.0-dev.201xxxxx)

Code

The following does not work

declare let foo: { hasError: true } | { hasError: false, value: any };
function test() {
  if (foo.hasError) return;
  foo.value; // Error
}

But the following does:

declare let foo: { hasError: true } | { hasError: false, value: any };
function test() {
  if (foo.hasError == true) return;
  foo.value; // Error
}

Expected behavior:
No error in both examples

Proof
image
image

BTW. Just wanted to say ... not related to this ... loving mapped types. So .. much .. power 🌹

@gcnew
Copy link
Contributor

gcnew commented Dec 15, 2016

I've run into this as well. It happens only if strictNullChecks is off. See #10564.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 21, 2016
@RyanCavanaugh
Copy link
Member

strictNullChecks is needed for this code to work because we can't rule out null or undefined as hasError values and thus can't narrow in the remainder of the function body

@basarat
Copy link
Contributor Author

basarat commented Dec 21, 2016

Closing in favor of #10564 🌹

@basarat basarat closed this as completed Dec 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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants