microsoft / microsoft/TypeScript

Easier use of indexed access types on nullable/optional types

Open
#59,293 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.