microsoft / microsoft/TypeScript

Easier use of indexed access types on nullable/optional types

Aperta
#59,293 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
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
⭐ 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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. 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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.