The axios library defines the get
function as shown in the code snippet below:
get<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
interface AxiosResponse<T = any, D = any> {
data: T;
status: number;
statusText: string;
headers: AxiosResponseHeaders;
config: AxiosRequestConfig<D>;
request?: any;
}
A custom PaginatedApiResponse
type has been defined for handling paginated results:
type PaginatedApiReponse<T = any, D = any> = AxiosResponse<T, D> & {
headers: AxiosResponseHeaders & {
'pagination-count': number,
'pagination-limit': number,
'pagination-page': number,
};
}
The intention is to utilize this custom type for auto-complete and type checking purposes:
axios.get<Product[], PaginatedApiReponse>('/api/producs'):
However, res.data
does not have the correct typing of
Product[]</code. Interestingly, the <code>headers
specified in PaginatedApiResponse
work correctly. To make it work, I need to also specify the second parameter like this:
axios.get<Product[], PaginatedApiReponse<Product[]>>('/api/producs'):
The question arises - why is the repetition of T
necessary when using a custom type?