Skip to content

unable to set response type on instance method #4223

@jelleschutter

Description

@jelleschutter

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

-

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions