Hello World
Hello World

Reputation: 154

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 ?

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)

Upvotes: 0

Views: 40

Answers (1)

chenc
chenc

Reputation: 331

I think you want expression is

type Valid = keyof typeof data.games

Upvotes: 1

Related Questions