question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with booleans in if-else statement

See original GitHub issue

It’s actually hard to describe the problem in the title in couple of words. You can find the playground here.

  1. I found out the similar problem in TypeScript (link to the my comment in the issue). I’m not sure if it is a limitation of strong type checking or a bug. I extect true in “else” part since we already handle false in “if” part.
  2. In my TypeScript example I can see that type of field (on the top level of the result object) is true and field’s type inside onClick function is boolean | undefined, but in the Hegel exemple both fields have boolean | undefined type. Which behavior is more correct?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
thecotnecommented, Apr 29, 2020

for me x === true or x !== true is very good way to convert any value to boolean everything that’s not true will become false (it matches Boolean constructors behavior for undefined and null but not for strings and numbers or arrays etc)

if you want true for everything that’s not boolean you can use x !== false or x === false

function foo(value: ?boolean): boolean {
  if (value !== true) {
    return false
  }

  return value
}

try

2reactions
ikabirovcommented, Apr 29, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditionals with if/else & Booleans | AP CSP (article)
The condition is a Boolean expression: an expression that evaluates to either true or false . Boolean values are another type of data...
Read more >
If Statements and Booleans
The test can be any expression that evaluates to a boolean value – true or false – value (boolean expressions are detailed below)....
Read more >
Correct use of "IS NOT" "OR" & "AND" boolean in nested ifelse
Output has issue with state == "w" code. It's not recognizing my attempted "IS NOT" and pastes "region_1" instead of NA (see lines...
Read more >
Booleans - Manual - PHP
It does not say "a boolean expresses a 0 or 1". It's true that symbolic constants are specifically designed to always and only...
Read more >
Boolean on if statement not working - Unity Answers
Your "If" statement is correct and should work. As long as both conditions are true. So the collider tag could be something other...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found