microsoft / microsoft/TypeScript

Easier use of indexed access types on nullable/optional types

Offen
#59,293 3 Kommentare 0 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

🔍 Search Terms

index access types optional nullable

✅ Viability Checklist
⭐ Suggestion

I would like a more succint way to access types from a nested object that contains nullable fields.

Possibly by allowing ! in a type-level position as a shorthand for NonNullable<T>

📃 Motivating Example

Generated types representing API responses often contain useful types that you'd prefer to extract rather than repeating them elsewhere. This comes up quite often for me when using graphql codegen on a query with a deeply nested response payload.

eg.

type APIResponse = {
	items: {
		id: string;
		author?: {
            id: string;
			name: string;
            status: "owner" | "admin" | "contributor" | "freeloader";
		}
	}[];
	pagination: {
		next_page: string;
	}
}

This example is a bit contrived, but is fairly representative of the general problem.

If I try and extract the status property, this doesn't work:

type APIAuthor = APIResponse['items'][0]['author']['status'];
//     Property 'status' does not exist on type '{ id: string; name: string; status: "owner" | "admin" | "contributor" | "freeloader"; } | undefined'.

Unless I wrap with NotNullable, and then it does.

type APIAuthor = NonNullable<APIResponse['items'][0]['author']>['status'];

Conceptually, I think the ! operator could work nicely here, giving us

type APIAuthor =APIResponse['items'][0]['author']!['status'];
💻 Use Cases

I've covered this a bit under the "motivating example" section, so maybe I've misused the headers a bit.

This comes up quite often for me when using graphql codegen on a query with a deeply nested response payload.

My current workaround is use of NonNullable, but when there's multiple layers of optionality it gets very difficult to follow.

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

Beginne mit dem motivierenden APIResponse-Beispiel und vergleiche die vorgeschlagene syntax ! auf Typebene mit dem bestehenden NonNullable-Workaround. Ermittle das beabsichtigte Verhalten für verschachtelte optionale und nullable indexierte Zugriffe und definiere anschließend Akzeptanzfälle, die zeigen, dass die Kurzschreibweise denselben extrahierten Typ erzeugt, ohne die JavaScript-Ausgabe zu verä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.