Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error TS2339: Property 'data' does not exist on type 'Codec' #6061

Closed
1 task done
ltfschoen opened this issue Jan 10, 2025 · 3 comments
Closed
1 task done

error TS2339: Property 'data' does not exist on type 'Codec' #6061

ltfschoen opened this issue Jan 10, 2025 · 3 comments

Comments

@ltfschoen
Copy link
Contributor

ltfschoen commented Jan 10, 2025

  • Bug report
  • What is the current behavior and expected behavior?

If I create this basic example ltfschoen/open-polkadot-js-library@81a0fe8, where I'm specifically using the latest @polkadot/api version 15.2.1, and @polkadot/keyring version 13.3.1, the README file for the keyring here https://github.com/polkadot-js/common/blob/v13.3.1/packages/keyring/README.md#usage says to import it with import Keyring from '@polkadot/keyring';, but if I do that it gives error:

  Type 'typeof import("/>path>/open-polkadot-js-library/packages/example/node_modules/@polkadot/api/index")' has no construct signatures.

21   const keyring = new Keyring({ type: 'sr25519' });

So I think that README should be changed to be as shown in https://polkadot.js.org/docs/api/examples/promise/make-transfer, where you import it with `import { Keyring } from '@polkadot/keyring';


**UPDATE** Actually, I'm not sure what I changed, but now I can access it successfully using both `import Keyring from '@polkadot/keyring';` and ``import { Keyring } from '@polkadot/keyring';`

I've created a PR here https://github.com/polkadot-js/common/pull/1970 for that.

But if I import it that way so I can use the `Keyring` constructor, and follow this basic example https://polkadot.js.org/docs/api/examples/promise/listen-to-balance-change of checking the balance of an account by providing their SS58 address string as an argument:

let { data: { free: previousFree }, nonce: previousNonce }
= await api.query.system.account('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');

It outputs error:

src/index.ts:30:9 - error TS2339: Property 'data' does not exist on type 'Codec'.

30 let { data: { free: previousFree }, nonce: previousNonce } = await api.query.system.account('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');
~~~~


And if I check the response it outputs:

let res = await api.query.system.account('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');
console.log(res.toJSON());

It outputs `{nonce: 0, consumers: 0, providers: 0, sufficients: 0, data: {…}}`

I can access the values of `nonce` and `data` as follows:

let res = await api.query.system.account(account.address);
console.log(JSON.parse(res.toString()).data);


But why doesn't it work if I just run it the way it is shown in the example:

let { data: { free: previousFree }, nonce: previousNonce }
= await api.query.system.account('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');


* **Please tell us about your environment:**


  - Version: Sonoma 14.5
  - Environment: macOS 

    - [X] Node.js
    - [ ] Browser
    - [ ] Other (limited support for other environments)

  - Language:

    - [ ] JavaScript
    - [X] TypeScript (include tsc --version)
    - [ ] Other
@ltfschoen
Copy link
Contributor Author

So instead of being able to just do this like in the Polkadot.js API examples:

let { data: { free: previousFree }, nonce: previousNonce } = await api.query.system.account(ALICE);
console.log(`${ALICE} has a balance of ${previousFree}, nonce ${previousNonce}`);

I had to do this instead:

  let res = await api.query.system.account(ALICE);
  if (!res) {
    console.log("Error retrieving balance");
  }
  let { data: { free: previousFree }, nonce: previousNonce } = JSON.parse(res.toString());
  console.log(`${ALICE} has a balance of ${previousFree}, nonce ${previousNonce}`);

@ltfschoen
Copy link
Contributor Author

Oh it's because I'm using TypeScript and the docs are for vanilla JS

Just have to add a type like any:

let { data: { free: previousFree }, nonce: previousNonce }: any = await api.query.system.account(ALICE);

@polkadot-js-bot
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Jan 17, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants