microsoft / microsoft/TypeScript

Syntactic checks against comparing to null/undefined in strictNullChecks mode

Offen
#60,425 7 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Experimentation Needed Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔍 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Überprüfe das vorgeschlagene Verhalten zusammen mit Issue #11920 und Pull Request #59217, die verwandte Ansätze für syntaktisch als nicht-nullish bekannte Ausdrücke definieren. Als abgeschlossen gilt die Aufgabe, wenn strictNullChecks die angeforderten unnötigen null/undefined-Vergleiche meldet, ohne das erzeugte JavaScript oder das Laufzeitverhalten zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.