-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Open
Labels
In DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
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
💻 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
Metadata
Metadata
Assignees
Labels
In DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript