microsoft / microsoft/TypeScript

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

Aperta
#45,667 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci gli esempi nel TypeScript Playground collegato e confronta le diagnosi esistenti per fn, !fn e i relativi confronti booleani. Quindi segui la logica del checker di TSC per le espressioni booleane costanti, aggiungi la coverage per il caso mancante !fn e verifica che venga emesso l'avviso previsto senza introdurre regressioni nei casi esistenti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.