chaeyk
chaeyk

Reputation: 591

Use typescript generic with class member type

interface MyState {
  balances: { [address: string]: BN };
}

const [ state, setState ] = useState</* what should I do? */>({});

I want to pass type of MyState.balances to useState's generic type like useState<typeof MyState.balances>({}), but it didn't work.

How can I do that?

Upvotes: 1

Views: 41

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97331

You can use an indexed access type:

interface MyState {
  balances: { [address: string]: BN };
}

const [ state, setState ] = useState<MyState['balances']>({});

Upvotes: 1

Related Questions