Use typescript generic with class member type

Viewed 29
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?

1 Answers

You can use an indexed access type:

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

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