Example:
interface IFetcher {
response: any
error: any
}
const Fetcher = (url: string): IFetcher => {
// loads of code here
return { response, error }
}
Fetcher function returns a response and error, how can i dynamically type my response and error objects, like using some dynamic type arguments?
If i consume my function:
const { response, error } = Fetcher('https://example.com')
I can type it:
const { response, error }: { response: MyCustomInterface, error: MyErrorInterface } = Fetcher('https://example.com')
I need to type response and error differently, depending on the use case. But above example looks kind of ugly. What's the better way to approach this?
UPDATED CODE
interface IFetcher {
(url: string, method: string, body: unknown, headers?: any): {
response: unknown;
error: unknown;
};
<T, U>(url: string, method: string, body: unknown, headers?: any): {
response: T;
error: U;
};
}