-
-
Notifications
You must be signed in to change notification settings - Fork 11.6k
unable to set response type on instance method #4223
Copy link
Copy link
Closed
Description
Describe the bug
When configuring the request method through the config object and using the axios instance method, we cannot define the response type returned by the request.
To Reproduce
In version 0.22 I used the following code:
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
const config: AxiosRequestConfig = {
method: 'get',
url: 'https://api.github.com/users/jelleschutter/repos?per_page=1'
}
type RepositoryModel = {
full_name: string;
description: string;
}
axios(config).then((res: AxiosResponse<RepositoryModel[]>) => {
console.log(res.data);
});Which leads to the following error in 0.23:
TS2345: Argument of type '(res: AxiosResponse<RepositoryModel[]>) => void' is not assignable to parameter of type '(value: AxiosResponse<unknown, any>) => void | PromiseLike<void>'.
Types of parameters 'res' and 'value' are incompatible.
Type 'AxiosResponse<unknown, any>' is not assignable to type 'AxiosResponse<Repository[], any>'.
Type 'unknown' is not assignable to type 'Repository[]'.
14 |
> 15 | axios(config).then((res: AxiosResponse<RepositoryModel[]>) => {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16 | console.log(res.data);
17 | });
Expected behavior
This issue was resolved in 0.24 however the type can still not be explicitly set like in this example:
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
const config: AxiosRequestConfig = {
method: 'get',
url: 'https://api.github.com/users/jelleschutter/repos?per_page=1'
}
type RepositoryModel = {
full_name: string;
description: string;
}
axios<RepositoryModel[]>(config).then(res => {
console.log(res.data);
});Environment
- Axios Version: 0.23.0
- Node.js Version: 14.15.4
- OS: Windows 10
Additional context/Screenshots
-
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels