encodeFunctionData and similar with generics #2777
Replies: 2 comments 1 reply
-
Couple quick options for you in this TypeScript Playground. Cast in function bodyPerfectly fine and natural to cast internal function values. function generic<
abi extends Abi,
functionName extends ContractFunctionName<abi>,
>(
abi: abi,
functionName: functionName,
args: ContractFunctionArgs<abi, AbiStateMutability, functionName>,
) {
const callData = encodeFunctionData({
abi: abi as Abi,
functionName: functionName as string,
args: args as readonly unknown[],
})
} Use overload for type inferenceCreate one signature that handles type inference, while the implementation uses the non-inferred values. function generic<
abi extends Abi,
functionName extends ContractFunctionName<abi>,
>(
abi: abi,
functionName: functionName,
args: ContractFunctionArgs<abi, AbiStateMutability, functionName>,
): void
function generic(
abi: Abi,
functionName: string,
args: readonly unknown[] | undefined,
) {
const callData = encodeFunctionData({ abi, functionName, args })
} |
Beta Was this translation helpful? Give feedback.
-
I went through implementation and I see it shouldn't work in case I described in initial post as generally there could
With small refactor in viem it should work, however it would most probably need us to resign from using |
Beta Was this translation helpful? Give feedback.
-
H
I wanna use encodeFunctionData inside generic function
And here problem comes as typescripts gives complicated error which most important part is :
I checked utils implementation and seens that all the evil comes from how
UnionEvaluate
usesPrettify
https://github.com/wevm/viem/blob/main/src/types/utils.ts#L165Which is very useful in development but doesnt seem to work well with generics. Does one of you meet and solve this problem already?
Beta Was this translation helpful? Give feedback.
All reactions