microsoft / microsoft/TypeScript

Change property modifier in mapped type based on condition

Open
#32,562 4 comments 21 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Domain: Mapped Types Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

As I know there is no way to change property modifier of mapped types based on conditional types. Here is my use case:

export class MakeItRequired<T extends ModelValue<any>> {
    target: "MakeItRequired" = "MakeItRequired"
    constructor(public option: T) {    
    }
}

export type ModelValue<T> = 
    T extends StringConstructor ? string : 
    T extends NumberConstructor ? number : 
    T extends BooleanConstructor ? boolean :
    unknown

export type ModelFromSchema<T> = {
    [P in keyof T]?: 
        T[P] extends MakeItRequired<infer U> ? ModelValue<U> :
        ModelValue<T[P]>
}

export const schema = {
    id: new MakeItRequired(Number),
    firstName: String,
    lastName: String
}

export const type: ModelFromSchema<typeof schema> = {}

My goal is to have id non optional, while having others optional.
Additional screenshoot from vscode:

Screenshot 2019-07-25 22 59 49

Can we have this feature?

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 mapped-type and conditional-type example in the issue, especially the desired required id property and optional name properties. Investigate how TypeScript represents mapped property modifiers and conditional types, then determine the feature scope and how the example would demonstrate completion. No specific source file or test is named in the issue.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.