Skip to content

Commit

Permalink
Narrow AccountClient to it's appropriate type
Browse files Browse the repository at this point in the history
  • Loading branch information
quellen-sol committed Mar 21, 2023
1 parent 5e13431 commit b2bba5c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- spl: Add metadata wrappers `approve_collection_authority`, `bubblegum_set_collection_size`, `burn_edition_nft`, `burn_nft`, `revoke_collection_authority`, `set_token_standard`, `utilize`, `unverify_sized_collection_item`, `unverify_collection` ([#2430](https://github.com/coral-xyz/anchor/pull/2430))

### Fixes
- ts: Narrowed `AccountClient` type to it's appropriate account type ([#2440](https://github.com/coral-xyz/anchor/pull/2440))

### Breaking

Expand Down
15 changes: 7 additions & 8 deletions ts/packages/anchor/src/program/namespace/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Provider, { getProvider } from "../../provider.js";
import { Idl, IdlAccountDef } from "../../idl.js";
import { Coder, BorshCoder } from "../../coder/index.js";
import { Subscription, Address, translateAddress } from "../common.js";
import { AllAccountsMap, IdlTypes, TypeDef } from "./types.js";
import { AllAccountsMap, IdlAccounts } from "./types.js";
import * as pubkeyUtil from "../../utils/pubkey.js";
import * as rpcUtil from "../../utils/rpc.js";

Expand All @@ -26,7 +26,7 @@ export default class AccountFactory {
programId: PublicKey,
provider?: Provider
): AccountNamespace<IDL> {
const accountFns: AccountNamespace = {};
const accountFns = {} as AccountNamespace<IDL>;

idl.accounts?.forEach((idlAccount) => {
const name = camelCase(idlAccount.name);
Expand All @@ -39,7 +39,7 @@ export default class AccountFactory {
);
});

return accountFns as AccountNamespace<IDL>;
return accountFns;
}
}

Expand Down Expand Up @@ -68,15 +68,14 @@ type NullableIdlAccount<IDL extends Idl> = IDL["accounts"] extends undefined
* For the full API, see the [[AccountClient]] reference.
*/
export type AccountNamespace<IDL extends Idl = Idl> = {
[M in keyof AllAccountsMap<IDL>]: AccountClient<IDL>;
[N in keyof AllAccountsMap<IDL>]: AccountClient<IDL, N>;
};

export class AccountClient<
IDL extends Idl = Idl,
A extends NullableIdlAccount<IDL> = IDL["accounts"] extends undefined
? IdlAccountDef
: NonNullable<IDL["accounts"]>[number],
T = TypeDef<A, IdlTypes<IDL>>
N extends keyof IdlAccounts<IDL> = keyof IdlAccounts<IDL>,
A extends NullableIdlAccount<IDL> = NullableIdlAccount<IDL>,
T = IdlAccounts<IDL>[N]
> {
/**
* Returns the number of bytes in this account.
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/anchor/src/program/namespace/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type AllAccounts<IDL extends Idl> = IDL["accounts"] extends undefined
* Returns a type of instruction name to the IdlInstruction.
*/
export type AccountMap<I extends IdlTypeDef> = {
[K in I["name"]]: I & { name: K };
[K in I["name"]]: I;
};

/**
Expand Down

0 comments on commit b2bba5c

Please sign in to comment.