How to get a generic type work with function variables? Following example doesn't work, but the idea is to have a generic function type.
type Request<T> = (input: RequestInfo, init: RequestInit) => Promise<T>
export const request: Request<T> = async <T>(input, init) => { // the second T is not recognized
return fetch(input, init).then(res => {
if (res.status < 300) {
return res.json()
}
return res.json().then(Promise.reject)
})
}
export const getRequest: Request<T> = <T>(input, init) => request<T>(input, { ...init, method: "GET" })
Shows error: Exported variable 'request' has or is using private name 'T'.