microsoft / microsoft/TypeScript
Easier use of indexed access types on nullable/optional types
Dieses Issue hat noch niemand übernommen.
- 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
- 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 isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Ö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