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

Typing with enum field and a random string as key

I have an enum like this

export enum Test {
  A = 'A',
  B = 'B',
  C = 'C'
}

I want a type that can have fields for each field of this enum A, B, C but also with the same fields concatenated with a string like ‘A_2’, ‘B_2’, ‘C_2’

So for the A,B,C part I got this type

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 TestExtended =  { [p in keyof typeof Test]: string } 

How can I complete it to get ‘A_2’, ‘B_2’, ‘C_2’ ?

>Solution :

You can remap keys to be concatenated with _2:

type TestExtended =  { [P in keyof typeof Test as `${P}_2`]: string };
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