microsoft / microsoft/TypeScript
Easier use of indexed access types on nullable/optional types
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the motivating APIResponse example and compare the proposed type-level ! syntax with the existing NonNullable workaround. Determine the intended behavior for nested optional and nullable indexed accesses, then define acceptance cases showing that the shorthand produces the same extracted type without changing JavaScript output.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100