Skip to content

POST call expected response body type also set to request body type #4132

@peetjvv

Description

@peetjvv

Describe the bug

When specifying an expected response.data type to the post method it also sets that expected type to the request body. Also, when not specifying a type, the type for the response.data gets inferred to be the same as the type of the request body. This leads to typescript errors in VS Code and during compile time when the response and request bodies in code doesn't have the same type (which is usually the case). This was not an issue in v0.21.4 - which I reverted to in order to get around this issue.

To Reproduce

  1. Install axios v0.22.0
  2. Add a type for the response.data like in this example:
axios.post<ExpectedResponseBody>(`post url`, { foo: "bar" }, {});

Also look at the CodeSandbox example here

Expected behavior

When specifying the expected type of the response.data in the post call, the request body shouldn't have the same expected type. And the inferred type for the request body shouldn't automatically get applied to response.data.

Environment

  • Axios Version 0.22.0
  • Adapter N/A
  • Browser N/A
  • Browser Version N/A
  • Node.js Version: 14.17.3
  • OS: Win 10
  • Additional Library Versions: Typescript 4.4.3

Additional context/Screenshots

Type for post method in axios v0.21.4
image

Type for post method in axios v0.22.0. Note that T is also set to request data and config in this version - which is what I believe is the cause of the issue.
image

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