microsoft / microsoft/TypeScript
`!constant` in boolean expression not reported (function not called error)
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere die Beispiele im verlinkten TypeScript Playground und vergleiche die vorhandenen Diagnosen für fn, !fn und ihre booleschen Vergleiche. Verfolge anschließend die TSC-Checker-Logik für konstante boolesche Ausdrücke, füge Abdeckung für den fehlenden Fall !fn hinzu und verifiziere, dass die erwartete Warnung ausgegeben wird, ohne die bestehenden Fälle zu beeinträchtigen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100