microsoft / microsoft/TypeScript

Introduce the opposite of the non-null assertion

Offen
#28,375 3 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Search Terms

non-null assertion opposite

Suggestion

The opposite of the "non-null assertion", used to force the compiler to believe that something may be undefined.

The most obvious syntax would be a "?" postfix operator, similar to how the non-null assertion is a "!" postfix operator. This would be consistent with the use of "?" for optional function params and object properties (which adds "undefined" to the type).

Use Cases

Indexing into arrays and "dictionary" objects with indexes/keys that are expected to potentially not exist.

Arrays and dictionary objects (an object type with the string index defined) are typically defined as having items whose type does NOT include undefined, even though it is technically possible (and valid) to use a non-existent index/key and get an undefined value as a result. In most situations, this avoids unnecessary/tedious non-null assertions, because arrays are often iterated (indexes guaranteed to exist), and dictionary objects are often used only to lookup keys that are guaranteed to exist.

However, in situations where an "untrusted" index/key is used on an array/dictionary, there is no convenient way to alter the type of the expression to indicate that the programmer is aware that the result may be undefined:

Examples

declare const array: Date[];
declare const untrustedIndex: number;

// Type is Date, but it may actually be undefined at runtime.
const item = array[untrustedIndex];

// To get useful "correct" typing, I must explicitly cast, duplicating the type 
// of the item and adding "| undefined" 
const item2 = array[untrustedIndex] as Date | undefined;

// Using the new proposed "possibly undefined assertion"
// Type is Date | undefined
const item3 = array[untrustedIndex]?;

Checklist

My suggestion meets these guidelines:

  • [?] This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)

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

Es werden keine Repository-Dateien, Tests oder Einstiegspunkte genannt. Beginne damit, die vorgeschlagene Postfix-Syntax sowie die Beispiele für Arrays und Dictionaries zu prüfen, und ermittle anschließend die Compiler-Bereiche, die Postfix-Assertions und den Zugriff über Indizes behandeln. Als abgeschlossen gilt die Aufgabe, wenn ein abgestimmtes Design und eine Implementierung vorliegen, die anhand des beschriebenen Typisierungsverhaltens validiert wurden.

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
Muss geklärt werden
Anfängerfreundlichkeit
20/100

Neue Issues direkt in Ihr Postfach

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