Problem with booleans in if-else statement
See original GitHub issueIt’s actually hard to describe the problem in the title in couple of words. You can find the playground here.
- 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 handlefalse
in “if” part. - In my TypeScript example I can see that type of
field
(on the top level of theresult
object) istrue
andfield
’s type insideonClick
function isboolean | undefined
, but in the Hegel exemple both fields haveboolean | undefined
type. Which behavior is more correct?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
for me
x === true
orx !== true
is very good way to convert any value to boolean everything that’s not true will become false (it matchesBoolean
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 usex !== false
orx === false
try
simple repro https://hegel.js.org/try#C4TwDgpgBAkgdmArsKBeKBvAUFKAzASwgBsATALigH4AjAezuIgEM4BuHKVu4ACwgBOAMSJlKAZ2ACCcAOYcAvhyx5EcAMbACdOPgYAKUs2DMYwCAFtK8JMACUmTgTxR9AQgBCDJq0PHT5hYAdIQkpHZ2nNi4uAIQwIgCcJwKWJzqOpL4ohT0jCy66EYmZpYhOVgKQA
it’s works if use
dataItem.field === undefined || dataItem.field === false
instead of!Boolean(dataItem.field)