1

No types for blockchain query responses are available in our IDE. Doing any query like this

await blockchain.api.query.assets.asset(id, (res) => {
    const info = res.toJSON()
    console.log("asset info", info)
})

the res variable is undefined.

i have my types available and can see api.query.assets and all the available functions like .asset but there are no return types. We have to manually type them like

const info:PalletAssetsAssetDetails = res.toJSON()
1
  • ok so it turns out we were stuck on an old version of the @polkadot/typegen and it was messing things up We updated to current version and its working for base generic substrate chain now. Next we gotta get our custom chain working. Oct 2, 2023 at 21:17

1 Answer 1

1

What you are missing is to apply augmentation, for that you have to import '@polkadot/api-augment' into your codebase entry point before any imports from the API itself:

import "@polkadot/api-augment"; 

See the docs from PolkadotJS: Since upgrading to the 7.x series, TypeScript augmentation is missing.

Now you will see that res has a type: Option<PalletAssetsAssetDetails> (An Option attempts to mimic the Rust approach of having None and Some available), you can get your values like that:


api.query.assets.asset(id, (res: any) => {
      const asset = res.unwrap();
      console.log(asset.owner.toString());
}
5
  • the toHuman doesnot give me any Types. I am using toHuman for the metadata like: name: res.name.toHuman() as any[][0] I still need the types from somewhere and the query response does not have them. The response is types as any Sep 27, 2023 at 23:14
  • Sorry you were right, I misunderstood what you were looking for. I have edited my response
    – Alex Bean
    Sep 28, 2023 at 13:14
  • i have the import "@polkadot/api-augment" in my app.tsx before importing anything and no types on the response... Oct 2, 2023 at 20:43
  • at first we were following this example github.com/polkadot-js/docs/tree/master/docs/api/examples/… Oct 2, 2023 at 20:44
  • 1
    ok so it turns out we were stuck on an old version of the @polkadot/typegen and it was messing things up. it works for the generic substrate chain now. Next we gotta get our custom chain working. thank you. Oct 2, 2023 at 21:16

Your Answer

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

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