#Requiring optional parameters
21 messages · Page 1 of 1 (latest)
export type WalletClient<
A demo might be helpful, looks like it should work to me.
If you hover over x it will say that it the type includes | undefined:
import { WalletClient } from "viem";
type LoadedClient = Omit<WalletClient, 'account'> & Required<Pick<WalletClient, 'account'>>;
const test = (client: LoadedClient) => {
const x = client.account;
}
you maybe have exactOptionalPropertyTypes enabled and WalletClient explicitly says | undefined?
if you could make a reproduction on the playground that'd probably help
I thought you can't pull packages onto the playground?
And I don't have that set in my tsconfig, seems like the default is false?
export type WalletClient<
transport extends Transport = Transport,
chain extends Chain | undefined = Chain | undefined,
account extends Account | undefined = Account | undefined,
rpcSchema extends RpcSchema | undefined = undefined,
> = Prettify<
Client<
transport,
chain,
account,
rpcSchema extends RpcSchema
? [...WalletRpcSchema, ...rpcSchema]
: WalletRpcSchema,
WalletActions<chain, account>
>
>
you can
it might be up to the package's tsconfig? not sure on that tbh
Oh all my searching said you couldn't, can you tell me how to do that so I can get you an example?
just import it
Oh I had no clue
Preview:```ts
import {Account, WalletClient} from "viem"
type LoadedClient = Omit<WalletClient, "account"> &
Required<Pick<WalletClient, "account">>
function useAccount(acc: Account) {}
function test(client: LoadedClient) {
useAccount(client.account)
}```
You can see that it still says that client.account may be undefined
WalletClient.account is Account | undefined. It is a required property, not an optional property.
And Required makes properties required but doesn't affect values of undefined
You can do
type LoadedClient = WalletClient & { account: Exclude<WalletClient['account'], undefined> }
or
type LoadedClient = {
[K in keyof WalletClient]: K extends 'account'
? Exclude<WalletClient[K], undefined>
: WalletClient[K]
}
@wild halo
Thanks so much @slate herald , I wasn't aware of those distinctions. Appreciate your help!