microsoft / microsoft/TypeScript
Easier use of indexed access types on nullable/optional types
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
🔍 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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par l’exemple motivant APIResponse et comparez la syntaxe ! proposée au niveau des types avec le workaround NonNullable existant. Déterminez le comportement attendu pour les accès indexés optionnels et nullable imbriqués, puis définissez des cas d’acceptation montrant que la forme abrégée produit le même type extrait sans modifier la sortie JavaScript.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100