2

Is it correct to be typing queryMulti return types such as in the following example, providing the types at the function call rather than inside the callback params? (For brevity I have used any for concrete return types):

const unsub: () => void = await api.queryMulti<
      [any, Option<any>, Option<any>]
    >(
      [
        [api.query.system.account, address],
        [api.query.staking.bonded, address],
        [api.query.staking.nominators, address],
      ],
      async ([{ data }, bonded, nominations]): Promise<void> => {
        const _account: any = {
          address,
        };

Note: An awesome update to the Polkadot JS API docs would be to be able to toggle between JavaScript and TypeScript for each of the examples, providing fully typed use cases and prevent developers from having to browse the PJS Apps codebase.

1 Answer 1

4

The correct way is certainly to pass the generic params as you indicated and as appears in the signature. (That should be available in your editor, obviously if it does that kind of thing ala VSCode and friends).

The following signatures are available -

<T extends Codec[]>(calls: QueryableStorageMultiArg<ApiType>[], callback: Callback<T>): UnsubscribePromise;
<T extends Codec[]>(calls: QueryableStorageMultiArg<ApiType>[]): Promise<T>;

So in the example you've listed, it would be akin to the following on a Kusama node -

api.queryMulti<[
  FrameSystemAccountInfo,
  Option<AccountId32>,
  Option<PalletStakingNominations>
]>([
  [api.query.system.account, address],
  [api.query.staking.bonded, address],
  [api.query.staking.nominators, address]
], ... // rest goes here

I think there would be a way to actually use TS to extract these from the parameters passed as the calls, it feels like a "simpler" problem to solve than what was done already with the strings from createType. Been thinking about it and have not quite put my hands on the keyboard to attempt it (it would be my first foray into extracting-stuff-from-args in TS), but it is something I actually want to contribute.

1
  • 2
    Indeed, think it is simpler than existing extraction. I logged it to keep it in the queue as a reminder - github.com/polkadot-js/api/issues/4875 (PRs really welcome if you get this right, I actually need it quite a bit and would make my life simpler)
    – Jaco
    May 30, 2022 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.