The parameters for the request
function are the same as those in the axios.request()
method.
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
async function request<T = any, R = AxiosResponse<T>>(config: AxiosRequestConfig): Promise<R> {
const r = await axios.request<T, R>(config);
return r;
}
async function json<T = any, R = AxiosResponse<T>>(config: AxiosRequestConfig): Promise<T> {
const response = await request<T, R>(config);
return response.data; // TSC throws error here
}
I expected the TS type of response
to be AxiosResponse
, but it actually is Awaited<R>
. I also anticipated the type of response.data
to be T
(AxiosResponse['data']
).