Using Array and Boolean in condition does not trigger a warning
See original GitHub issueTypeScript Version: 3.8.0-dev.20200115
Search Terms: overlap boolean vs array condition
Code
function isAllowed() {
const myCondition = false;
if (myCondition === false) {
return [];
}
return true;
}
if (isAllowed()) {
// boolean and array will results in a "true" condition
console.log('allowed');
} else {
// never reached
console.log('not allowed');
}
Expected behavior:
- Seeing a message saying something like
This condition can produce unexpected results since the types 'boolean' and 'array' have no overlap
(not really good but you get the idea)
Actual behavior: I won’t say that it’s a bug, but something that could be improved. I know that it’s valid javascript but this a bug in the code that should/could be prevented by Typescript
- Typescript does not complain that comparing boolean and array can results in bad condition
Already fixable by
- It does complain if you write
if (isAllowed() === true)
but it’s easy to not do it.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Python 3.7 boolean indexing *warning* using 'list'
We do get the warning if we try to index with a numpy boolean: In [26]: abs_A[i]*(-1, 1)[np.array(sign_A)[i]] /usr/local/bin/ipython3:1: ...
Read more >JavaScript Tips: Using Array.filter(Boolean) - Mike Bifulco
Boolean is a helper class in JavaScript which can be used to test whether a given value or expression evaluates to true or...
Read more >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 >Logical NOT (!) - JavaScript - MDN Web Docs - Mozilla
The logical NOT ( ! ) (logical complement, negation) operator takes truth to falsity and vice versa. It is typically used with boolean...
Read more >Booleans - Manual - PHP
To specify a bool literal, use the constants true or false . Both are ... which won't work in JavaScript where you need...
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
So thinking these through (this is my gut check, please disagree):
boolean | undefined
- bad (conflatesfalse
andundefined
)boolean | object
- bad (conflatestrue
and{}
)number | undefined
- bad (conflates0
andundefined
)object | number
- bad? (conflates1
and{}
)boolean | number
- bad? (conflates on both sides)object | undefined
- good (falsiness is unambiguous)The rule I can backport on to this intuition is “If there is more than one truthy value or more than one falsy value, those values should be of the same type”.
What’s your intuition?
In my experience, anyone attempting to do something like this:
are always making a mistake. The following is their intention:
I don’t think it ever makes sense to evaluate an array (or any object) directly as a boolean.