microsoft / microsoft/TypeScript
Optional Type Chaining
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Suggestion
🔍 Search Terms
optional type chaining syntax
✅ Viability Checklist
My suggestion meets these guidelines:
- 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 feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Support optional chaining syntax for types
📃 Motivating Example
The following pattern is so common that optional chaining syntax was introduced into JS:
type DeeplyNullableObject = undefined | {
key?: {
nestedKey?: {
deeplyNestedKey?: 'value'
}
}
}
const deeplyNullableObject: DeeplyNullableObject = {}
// cumbersome
deeplyNullableObject
? deeplyNullableObject['key']
? deeplyNullableObject['key']['nestedKey']
? deeplyNullableObject['key']['nestedKey']['deeplyNestedKey']
: undefined
: undefined
: undefined
// convenient!
deeplyNullableObject?.['key']?.['nestedKey']?.['deeplyNestedKey']
However, in TypeScript, accessing deeplyNullableObject?.key?.nestedKey?.deeplyNestedKey's type is still cumbersome (even with TypeScript's super helpful NonNullable utility type):
// cumbersome
type DeeplyNestedValue = NonNullable<
NonNullable<
NonNullable<
DeeplyNullableObject
>['key']
>['nestedKey']
>['deeplyNestedKey']
💻 Use Cases
Supporting optional chaining syntax for types would make working with these types just as easy as it is for their runtime counterparts:
// convenient!
type DeeplyNestedValue = DeeplyNullableObject?.['key']?.['nestedKey']?.['deeplyNestedKey']
It also allows developers to easily convert some runtime values declared using optional chaining to types by simply applying typeof, which is always satisfying and feels well within the spirit of TypeScript being elegantly consistent with JavaScript:
type DeeplyNestedValue = typeof deeplyNullableObject?.['key']?.['nestedKey']?.['deeplyNestedKey']
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 by reviewing the proposed optional type-chaining examples and TypeScript's linked design goals. The issue names no implementation files, entry points, or tests, so identifying the relevant type-system and parser areas would be part of the initial investigation. Done would require an agreed design plus implementation and coverage for the shown type expressions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100