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

Convert one type to another

I have a type like this:

type Popups = {
  'createUserDialog': TCreateUserDialogProperties,
  'updateUserDialog': TUpdateUserDialogProperties
};

I would like to convert this type to:

type Something = {
  name: 'createUserDialog',
  properties: TCreateUserDialogProperties
} | {
  name: 'updateUserDialog',
  properties: TUpdateUserDialogProperties
};

How I can do this, I’ve tried this solution:

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

type Something = {
  [K in keyof Popups]: {
    name: K,
    properties: Popups[K]
  }
};

but it is incorrect…

>Solution :

You’ve pretty much got it. I think this is what you want:

type Something = {
  [K in keyof Popups]: {
    name: K,
    properties: Popups[K]
  }
}[keyof Popups];
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