admin管理员组

文章数量:1390204

When using the viem library's createWalletClient function, you can optionally provide an account parameter (to hoist it), and thereafter it's not needed to be specified on calls like walletClient.writeContract().

In Typescript, viem exports a WalletClient type, but it is defined as having its account be optional. I have an helper function that always returns a WalletClient object with account set. How can I annotate the helper function so all calling modules know the account is already specified, and typescript stops telling me to add it into subsequent writeContract() calls?

The viem WalletClient type does take some generics parameters, but I cannot figure a concise way to specify "the account is set to an address value" using them.

When using the viem library's createWalletClient function, you can optionally provide an account parameter (to hoist it), and thereafter it's not needed to be specified on calls like walletClient.writeContract().

In Typescript, viem exports a WalletClient type, but it is defined as having its account be optional. I have an helper function that always returns a WalletClient object with account set. How can I annotate the helper function so all calling modules know the account is already specified, and typescript stops telling me to add it into subsequent writeContract() calls?

The viem WalletClient type does take some generics parameters, but I cannot figure a concise way to specify "the account is set to an address value" using them.

Share Improve this question edited Mar 17 at 4:37 MidnightLightning asked Mar 17 at 4:20 MidnightLightningMidnightLightning 6,9387 gold badges47 silver badges72 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

For a WalletClient that has an RPC connection (Transport), target chain, and address defined, the following type definition creates an alias for that:

export type ConnectedWalletClient = WalletClient<Transport, Chain, Account, undefined>

Having that be the type that the helper function returns, and being an exported type then allows other modules to use ConnectedWalletClient as a short alias, and have Typescript not require specifying the account parameter on subsequent writeContract calls.

本文标签: Typescript type for viem WalletClient with quotaccountquot definedStack Overflow