coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.69k stars 1.35k forks source link

ts: `AccountClient` isn't properly typed #1706

Closed callensm closed 11 months ago

callensm commented 2 years ago

Querying for accounts like program.account.myAccount.fetch(addr) should return an object that is strictly typed for the myAccount account type definition.

However, the client currently types the object as being the union of properties between all IDL defined account structs.

image

As seen in the screenshot, the typing on the listing account client includes the properties of other account types.

callensm commented 2 years ago

It is because of: https://github.com/project-serum/anchor/blob/master/ts/src/program/namespace/account.ts#L46 and https://github.com/project-serum/anchor/blob/master/ts/src/program/namespace/account.ts#L76

which are saying that the type if an arbitrary index of the accounts array in the IDL, allow the type definition to become the union of all available.

paul-schaaf commented 2 years ago

probably for the same reason, program.methods.accounts({ .. }) is also not typed properly

callensm commented 2 years ago

the MethodsBuilder.accounts function typings should have been fixed in a PR I submitted about a month ago.

paul-schaaf commented 2 years ago

it it fixed for you? its not working for me

callensm commented 2 years ago

ahhh, it works as expected as a chain onto the instruction, e.g.:

program
  .methods
  .initAccount()
  .accounts({ ... })
  // ...

but using it prior to the instruction would cause issues because the typings don't know which instruction has been selected yet to pull the account names for.

acheroncrypto commented 11 months ago

Fixed in https://github.com/coral-xyz/anchor/pull/2440