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

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!

Upvotes: 0

Views: 25

Answers (1)

Tushar Shahi
Tushar Shahi

Reputation: 20616

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

Upvotes: 1

Related Questions