Can someone help me out with this error? I'm having difficulty grokking the issue. I'm not sure what is the issue but I also wouldn't say that I'm an expert in Generics.
What I am looking to do is build a decorator function that will allow specialized behaviors to either be overridden or interjected into another function (when it allows but that isn't important). The withHooks function is a curried function (factory) that takes in another function that will/should specify it's own types and specifically it's return types.
This is the API
// TYPES
type DefReturns = string | number
type Route = <T = DefReturns>() => Promise<T>
interface TestRoute {
foo: string
}
// FUNCTION SIGNATURE
const withHooks = <T>(fn: Route) => async (): Promise<void> => { /* */ })
Here is the function that I'm using with the HoF above (withHooks(/* */)). The highlighted part is the error below.
const testRoute = withHooks(async (): Promise<TestRoute> => { /* */ })
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The TypeScript error
Argument of type '(/* */) => Promise<TestRoute>' is not assignable to parameter of type 'Route'.
Type 'Promise<TestRoute>' is not assignable to type 'Promise<T>'.
Type 'TestRoute' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'TestRoute'.ts(2345)
Thing is that I don't even think I typed out withHooks correctly. It's not that function that I think needs to be typed but rather it's fn: Route parameter but the following also gives an error so I'm not really sure how to fix this
export const withHooks = <T>(fn: Route<T>) => ...
^^^^^^^^
// Type 'Route' is not generic.