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.

Using Array and Boolean in condition does not trigger a warning

See original GitHub issue

TypeScript 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.

Playground Link: http://www.typescriptlang.org/play/?ts=3.8.0-dev.20200115&ssl=1&ssc=1&pln=15&pc=1#code/GYVwdgxgLglg9mABDAzgQQDYbgdwKYAmAFAJSIDeAUIohAilIgLYCeAwggTLAogLyIoAJxB4A3NWTBERVhzBceSPisEi8ZKjRpC8UEEKQBtALoSaAX0qTd+w4mABDDCnGUrlGNKKpM2fMQkmpJ0YChwGHgAdNgA5kQA5M7+hAkkEhaIeC54FCH0EdFxiWBwjMm4qenulEA

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
RyanCavanaughcommented, Jan 16, 2020

So thinking these through (this is my gut check, please disagree):

  • boolean | undefined - bad (conflates false and undefined)
  • boolean | object - bad (conflates true and {})
  • number | undefined - bad (conflates 0 and undefined)
  • object | number - bad? (conflates 1 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?

1reaction
mwgamblecommented, Feb 3, 2020

In my experience, anyone attempting to do something like this:

const myArray = functionThatReturnsVariableLengthArray();
if (myArray) {
    // do stuff
}

are always making a mistake. The following is their intention:

if (myArray.length > 0) {

I don’t think it ever makes sense to evaluate an array (or any object) directly as a boolean.

Read more comments on GitHub >

github_iconTop 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 >

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