I am writing some middleware with the following interface
interface Config {
    callApi<P> (api: ApiClient): Promise<P>,
    transformResponse?(payload: P): any
}
From the above,  you can see that the config takes a callApi function which will call an remote APi and return the promise-based result, and then and optional transformResponse that will receive the payload, and apply a transformation to it. The key part is that the transformResponse function should infer the type of the payload, based on the return type of the callApi call
Problem is that the above is not valid, because the P type inside transformResponse can not reach the same variable for the callApi function.
Is there any way to accomplish the above, without having to pass in the API response type like this
interface Config<ApiResponse> {
    callApi (api: ApiClient): Promise<ApiResponse>,
    transformResponse?(payload: ApiResponse): any
}
Example usage
const callCreateComment: Config = {
  callApi (api) => api.createComment(),
  transformResponse(payload) => ({ ...payload // infer the return type of the `api.createComment()` call })
}
 
    