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

Add null check section in type guard page #497

Closed
golopot opened this issue May 10, 2019 · 1 comment
Closed

Add null check section in type guard page #497

golopot opened this issue May 10, 2019 · 1 comment

Comments

@golopot
Copy link

golopot commented May 10, 2019

https://github.com/basarat/typescript-book/blob/master/docs/types/typeGuard.md

Cases like this:

function foo(a?: number, b?:number) {
  if (a) {
    // a is number now.
  }

  while (!b) {
    ...
  }
  // b is number now
}
@basarat
Copy link
Owner

basarat commented May 14, 2019

Thanks. Added a version 🌹

Minor, careful with falsy values e.g.

function foo(a?: number, b?:number) {
  if (a) {
    // a is number now.
  }
  else {
    // a is `number?` (instead of `undefined`) as 0 (a member of number) is also falsy. 
  }
 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants