microsoft / microsoft/TypeScript
Allow conditionally setting optional properties in a mapped type
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
conditional optional mapped type
Suggestion
Allow us to conditionally apply ? to a property in a mapped type
Use Cases
I have a function:
function myFunction(one: string, two: number, three: number){}
but I want to map the params to
function myFunction(one: string, two?: number, three?: number){}
Examples
Suggested approach:
type MapParams<T> = {
[P in keyof T]: T[P] extends string ? T[P] : | ?T[P]
}
(or something similar)
This is the closest I could come:
type MapParams<T> = {
[P in keyof T]: T[P] extends string ? T[P] : | T[P] | undefined
}
but that produces
function myFunction(one: string, two: number | undefined, three: number | undefined){}
myFunction("", undefined, undefined)
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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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
The issue provides no implementation files or tests; begin with the linked Playground and the mapped-type examples. Completion means supporting a way to conditionally make selected mapped properties optional, distinguishing omitted properties from properties explicitly set to undefined, with compiler tests covering the shown function shape.
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