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.

`!constant` in boolean expression not reported (function not called error)

See original GitHub issue

Bug Report

It is a common source of bugs that developers fail to call functions in boolean expressions. This, among other reasons, the compiler checks for constant boolean expressions. However, there seems to be a hole in the existing checks. TSC currently allows “!fn” where “fn” and “(!fn) == true” are not allowed. It would be beneficial to check this kind of expression as well.

function fn() { return 'abc'; }

if (fn) {}  // ERROR: This condition will always ...
if (!fn) {} // not reported
if (fn == true) {} // ERROR: This condition will always ...
if ((!fn) == true) {} // ERROR: This condition will always ...

🔎 Search Terms

function “condition always true”

function constant boolean expression

🕗 Version & Regression Information

unknown, presumably since the beginning of time, at least TS 3.3, related improvement in TS 3.7

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Truthy expressions

⏯ Playground Link

Playground link with relevant code

https://www.typescriptlang.org/play?ts=3.7.5&q=247#code/GYVwdgxgLglg9mABMMAKAlIg3ogTgUyhFyQHIBDAIwlIG5EBfAWAChWZhFUVMtmWOXAIQ9s-QdyQBeKYii4Q+XuM6pUIsJhlyFSsa1ZA

💻 Code

function fn() { return 'abc'; }

if (fn) {}  // ERROR: This condition will always ...
if (!fn) {} // not reported
if (fn == true) {} // ERROR: This condition will always ...
if ((!fn) == true) {} // ERROR: This condition will always ...

🙁 Actual behavior

No warning

🙂 Expected behavior

“This condition will always …” warning

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
concavelenzcommented, Sep 1, 2021

Detecting fn when the author intended fn() is valuable. TSC already catches “if (fn) …” is great, I’d hope that catching "if (!fn) " would also be seen as worthwhile as !x is a common use of a result value in a boolean expression.

0reactions
DanielRosenwassercommented, Sep 1, 2021

So this is definitely confusing, especially because the error messages are similar; but my understanding is

  • if (fn) is getting an error for always-defined functions. That’s expected.
  • if (!fn == true) is getting an error for something called comparability which checks for overlap between two types. The check is expected , but I think the result of !fn isn’t.

I think we’re trying to be “too smart” in the second example though. I would expect !fn would just become boolean (not false), and would pass the check.

Read more comments on GitHub >

github_iconTop Results From Across the Web

3.6 Boolean Expressions
When you are supplying a Boolean value, you can type either YES , ON , or TRUE for a true value, and NO...
Read more >
5-1 Boolean expressions
A boolean expression(named for mathematician George Boole) is an expression that evaluates to either true or false. Let's look at some common language...
Read more >
Laws of Boolean Algebra - Electronics Tutorials
Electronics Tutorial about the Laws of Boolean Algebra and Boolean Algebra Rules including de Morgans Theorem and Boolean Circuit Equivalents.
Read more >
Logical Expressions - Propositional Logic
If LE1 is a logical expression, then NOT LE1 is a logical expression, whose value is TRUE is LE1 has the value FALSE,...
Read more >
Conditionals with if/else & Booleans | AP CSP (article)
Computer programs also make decisions, using Boolean expressions ... the computer whether to execute the instructions inside the curly brackets or not.
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