Best practices - defining types when lib doesn't export the type

I'm writing a hook that wraps a transaction from another library. The library returns a Promise<SendTransactionResult>, but SendTransactoinResult is not exported from the library. I'm writing a handler for the promise but I'm struggling with what the best practice would be for defining the type that this callback should expect.
/*...inside hook...*/
// my function
const afterContractWrite = useCallback(
(result: <HowWouldYouDefineThisType?>) => {
onSettled?.();
return result.wait();
},
[onSettled]
);

//this method returns a Promise<SendTransactionResult> but the type is not exported from the lib so I can't just import it.
writeAsync?.()
.then(afterContractWrite)
/*...inside hook...*/
// my function
const afterContractWrite = useCallback(
(result: <HowWouldYouDefineThisType?>) => {
onSettled?.();
return result.wait();
},
[onSettled]
);

//this method returns a Promise<SendTransactionResult> but the type is not exported from the lib so I can't just import it.
writeAsync?.()
.then(afterContractWrite)
How would you handle this?
3 Replies
CuriouslyCory
CuriouslyCoryOP2y ago
I found Awaited<ReturnType<typeof writeAsync>>, but it's throwing a type error:
Type '((overrideConfig?: { recklesslySetUnpreparedArgs?: undefined; recklesslySetUnpreparedOverrides?: (Overrides & { from?: `0x${string}` | undefined; }) | undefined; } | undefined) => Promise<...>) | undefined' does not satisfy the constraint '(...args: any) => any'.
Type 'undefined' is not assignable to type '(...args: any) => any'.
Type '((overrideConfig?: { recklesslySetUnpreparedArgs?: undefined; recklesslySetUnpreparedOverrides?: (Overrides & { from?: `0x${string}` | undefined; }) | undefined; } | undefined) => Promise<...>) | undefined' does not satisfy the constraint '(...args: any) => any'.
Type 'undefined' is not assignable to type '(...args: any) => any'.
So, writeAsync can either be a function, or undefined, and the type error is complaining that it can't infer the return type of undefined since it's not a function. Any suggestions on how I would gate that so I can get the result assuming it's not undefined?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CuriouslyCory
CuriouslyCoryOP2y ago
Thanks! I'll give this a shot.
Want results from more Discord servers?
Add your server