microsoft / microsoft/TypeScript

Optional Type Chaining

Offen
#45,793 1 Kommentar 11 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Suggestion

🔍 Search Terms

optional type chaining syntax

✅ Viability 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. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Support optional chaining syntax for types

📃 Motivating Example

The following pattern is so common that optional chaining syntax was introduced into JS:

type DeeplyNullableObject = undefined | {
  key?: {
    nestedKey?: {
      deeplyNestedKey?: 'value'
    }
  }
}

const deeplyNullableObject: DeeplyNullableObject = {}

// cumbersome
deeplyNullableObject
  ? deeplyNullableObject['key']
    ? deeplyNullableObject['key']['nestedKey']
      ? deeplyNullableObject['key']['nestedKey']['deeplyNestedKey']
      : undefined
    : undefined
  : undefined

// convenient!
deeplyNullableObject?.['key']?.['nestedKey']?.['deeplyNestedKey']

However, in TypeScript, accessing deeplyNullableObject?.key?.nestedKey?.deeplyNestedKey's type is still cumbersome (even with TypeScript's super helpful NonNullable utility type):

// cumbersome
type DeeplyNestedValue = NonNullable<
  NonNullable<
    NonNullable<
      DeeplyNullableObject
    >['key']
  >['nestedKey']
>['deeplyNestedKey']

💻 Use Cases

Supporting optional chaining syntax for types would make working with these types just as easy as it is for their runtime counterparts:

// convenient!
type DeeplyNestedValue = DeeplyNullableObject?.['key']?.['nestedKey']?.['deeplyNestedKey']

It also allows developers to easily convert some runtime values declared using optional chaining to types by simply applying typeof, which is always satisfying and feels well within the spirit of TypeScript being elegantly consistent with JavaScript:

type DeeplyNestedValue = typeof deeplyNullableObject?.['key']?.['nestedKey']?.['deeplyNestedKey']

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

Beginnen Sie mit der Durchsicht der vorgeschlagenen Beispiele für optionales Type-Chaining und der verknüpften Designziele von TypeScript. In der Issue werden keine Implementierungsdateien, Einstiegspunkte oder Tests genannt; daher wäre die Identifizierung der relevanten Bereiche des Typsystems und des Parsers Teil der ersten Untersuchung. Als abgeschlossen würde eine abgestimmte Design sowie eine Implementierung und Abdeckung für die gezeigten Typsausdrücke voraussetzen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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