`!constant` in boolean expression not reported (function not called error)
See original GitHub issueBug 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
💻 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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >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
Detecting
fn
when the author intendedfn()
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.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 becomeboolean
(notfalse
), and would pass the check.