2

So I'm able to run tests just fine and I know for a fact that the typescript is picking up the types from my compiled program.

With that said, I have resorted to using let myAccount: Awaited<ReturnType<typeof program.account.myAccount.fetch>>; to get the type in my Typescript. How do I reference the types from my compiled program?

1 Answer 1

3

Your idl inside the generated typescript file should have the following format:

export type SomeProgram = {
  "version": "0.1.0",
  "name": "some_program",
  "instructions": [// Instructions here],
  "accounts:":[// Accounts defs here, what we'll be accessing below],
  "types":[// Custom types here],
  "errors":[//Errors here],
};

export const IDL: SomeProgram = {
  "version": "0.1.0",
  "name": "some_program",
  "instructions": [// Instructions here],
  "accounts:":[// Accounts here],
  "types":[// Custom types here],
  "errors":[//Errors here],
};

After you've validated this is the format, you should just be able to import it in your tests like below and get the type of some account, like this (assuming you want the account type called myAccountType):

import * as anchor from "@coral-xyz/anchor";
import { SomeProgram } from '../target/types/some_program'
let myAccount : anchor.IdlAccounts<SomeProgram>['myAccountType'] 
2
  • So if I use type MyAccount = Awaited<ReturnType<typeof program.account.myAccount.fetch>> I get a fully fledged object type with property names and such. It looks like {field0: type, field1: type}. If I use type MyAccount = MyProgram["accounts"][0], I get an object that looks like it has instructions on how to build MyAccount type. It looks like ` { name: "myAccount"; type: { kind: "struct"; fields: [{..},...]; } } `
    – techiejd
    Commented Mar 18 at 2:47
  • 1
    You're right, just adapted my answer & should now work in the way you want it to
    – McBain
    Commented Mar 20 at 3:53

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.