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 to make type as dynamic using typescript?

I have a dynamic data like,

const data = {games:{type: [], sport: [], category: []}}

And I have hardcoded type like type Valid = "type" & "sport" & "category"

How can I make type Valid dynamic based on the Object.keys(data.games) instead of hardcoding ?

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

I have tried the following,

type Valid = typeof Object.keys(data.games).join(' & ');

But this gives the error as,

Unexpected token, expected ";"

Please help me to get dynamic values for the type Valid.

Working Example:

Edit disable-dependent-dropdown-option-in-reactjs (forked)

>Solution :

I think you want expression is

type Valid = keyof typeof data.games
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