Home > database >  How can I covert all property to all optional in a type?
How can I covert all property to all optional in a type?

Time:10-19

I have a type like:

type A1 = {
 x: string,
 y: string
}

and I would like to convert it to

type A2 = {
 x?: string,
 y?: string
}

I have searched similar problems but could not find a good way to simply achieve this goal. Can somebody solve this problem? Thanks!

CodePudding user response:

Use Partial:

It constructs a type with all properties of Type set to optional.

type A1 = {
 x: string,
 y: string
}

type A2 = Partial<A1>;

Playground

  • Related