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

Typescript library has poor typing #3552

Open
marcospgp opened this issue Feb 11, 2025 · 0 comments
Open

Typescript library has poor typing #3552

marcospgp opened this issue Feb 11, 2025 · 0 comments

Comments

@marcospgp
Copy link

marcospgp commented Feb 11, 2025

I believe typing in the anchor TS library could be greatly improved

The way types are generated currently is a bit of a maze, and this complexity even surfaces to the user

For example, today it took me > 1h to figure out how to properly type an account retrieved when listening to account changes for a given program:

const request: IdlAccounts<Chainference>["inferenceRequestAccount"] = chainference.coder.accounts.decode(
  // @ts-expect-error: we have to access a private field here.
  chainference.account.inferenceRequestAccount._idlAccount,
  account.accountInfo.data
);

Not even chatgpt was a good help there, and docs for the anchor TS lib are not that comprehensive and don't mention this kind of functionality either.

For this situation the ideal would be for there to be a properly typed decode method that doesn't need any typing at all, or passing in the account name (which I am getting from an obscure private field there because having hardcoded magic strings is not a good idea - it could break silently if the program changes).

But I think this is just an illustration of the kinds of problems that are arising from the current typing implementation.

One can envision a much cleaner approach where instead of generating a huge type for the entire program IDL, separate types would be generated for each account type, for example.

Edit: Another example of poor typing would be the tooltip for calling a method:

Image

it should be clear what parameters are expected and what is returned, but the user is presented with a complex set of type annotations which take some time to decipher.

Edit 2: the following day I had to write this:

Image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant