Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading