I'm looking to create a helper function that can wrap an existing async function with a variable number of arguments, allowing me to call it like this:
const submit = wrapLoading(async (values: string[]) {
// await something with values
return true
})
submit(['1', '2']) // Call it
However, I've run into an issue where the return type is always Promise<any>
, even though in the example above, the function being wrapped returns a Promise<boolean>
. Here's my implementation. Can anyone help me understand why this is happening?
export const wrapLoading = <F extends(...args: any[]) => Promise<any>>(
process: F
) => async (...args: Parameters<F>) => {
isLoading.value = true
const result = await process(...args)
isLoading.value = false
return result
}
I've tried using another argument to specify the return type, but then it always returns a Promise<unknown>
. I'm struggling to find an elegant solution to this problem. Any assistance would be greatly appreciated!