microsoft / microsoft/TypeScript
Easier use of indexed access types on nullable/optional types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔍 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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con l’esempio motivante di APIResponse e confronta la sintassi ! proposta a livello di tipo con il workaround esistente NonNullable. Determina il comportamento previsto per gli accessi indicizzati opzionali e nullable annidati, quindi definisci casi di accettazione che dimostrino che la forma abbreviata produce lo stesso tipo estratto senza modificare l’output JavaScript.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100