Skip to content

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

@concavelenz

Description

@concavelenz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions