I'm doing a simple memoize function, what it does is that it takes a function and will do some magic where it caches the return result. But basically the types for the returned function for memoize is exactly the same as the given one. But I'm having errors which I don't know how to fix. When using the function while ignoring the errors I'm having, the types work.
I've made two functions to showcase my problem, from my perspective, both types should work but I'm getting closer with the second implementation, however it still doesn't work.
Here is a link to the TypeScript Playground but just in case it can expire I will post some code with some pictures that shows the errors:
function memoize1<F extends (...args: any[]) => Promise<any>>(func: F): F {
return (...args) => func(...args)
}
function memoize2<F extends (...args: any[]) => Promise<any>>(func: F): (...args: Parameters<F>) => ReturnType<F> {
return (...args) => func(...args)
}
const first = memoize1(() => {}) // Complains
const second = memoize1(async () => {}) // Works
const third = memoize2(() => {}) // Complains
const fourth = memoize2(async () => {}) // Works
Here are the error images, one for the first example and the second example