microsoft / microsoft/TypeScript

Syntactic checks against comparing to null/undefined in strictNullChecks mode

Aperta
#60,425 7 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Experimentation Needed Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 Search Terms

nullish, strictNullChecks, equals, undefined, null

✅ Viability Checklist
⭐ Suggestion

When strictNullChecks are enabled, disallow == and === comparisons to null/undefined for expressions that are syntactically known to not be undefined.

I'd imagine the definition of syntactically not null/undefined expressions would be similar to that from https://github.com/microsoft/TypeScript/pull/59217.

This is similar to https://github.com/microsoft/TypeScript/issues/11920, but I think would be easier to land because it won't error on defensive programming null checks.

📃 Motivating Example

TypeScript's typeof operator always returns a string, but it can be hard to spot the difference between checking "undefined" and just undefined in code like this:

if (typeof foo === undefined) {

In strictNullChecks mode, TypeScript will catch unnecessary comparisons like this, for expressions that at runtime it knows can never be null or undefined.

💻 Use Cases
  1. What do you want to use this for? catch user errors
  2. What shortcomings exist with current approaches? there's the https://typescript-eslint.io/rules/no-unnecessary-condition/, but that's type-based so does have the problem of flagging defensive programming. it's also nice to have things be in the compiler instead of a separate tool.
  3. What workarounds are you using in the meantime? nothing at the moment, if this is infeasible will probably create a custom lint check.

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

Esamina il comportamento proposto insieme all’issue #11920 e alla pull request #59217, che definiscono approcci correlati per espressioni sintatticamente note come non nullish. Il lavoro è completato quando strictNullChecks segnala i confronti non necessari tra null/undefined richiesti senza modificare il JavaScript emesso né il comportamento a runtime.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.