Home > Mobile >  How to make type as dynamic using typescript?
How to make type as dynamic using typescript?

Time:11-29

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 ?

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)

CodePudding user response:

I think you want expression is

type Valid = keyof typeof data.games
  • Related