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.
1 Answer
Reset to default 0For 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.