microsoft / microsoft/TypeScript
`!constant` in boolean expression not reported (function not called error)
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the examples in the linked TypeScript Playground and compare the existing diagnostics for fn, !fn, and their boolean comparisons. Then trace the TSC checker logic for constant boolean expressions, add coverage for the missing !fn case, and verify that the expected warning is emitted without regressing the existing cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100