Home > Net >  Use typescript generic with class member type
Use typescript generic with class member type

Time:07-11

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?

CodePudding user response:

You can use an indexed access type:

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

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