Skip to content

Axios response headers can be undefined (TS typing isn't strict enough) #4812

@gprst

Description

@gprst

Describe the bug

Axios response headers can actually be undefined. For instance, a response can sometimes contain the location header, and sometimes not.

To Reproduce

type Expense = {
  // ...
};

async function postExpense(projectId: string, expense: Omit<Expense, 'id'>): Promise<string> {
  const res = await axios.post(`/projects/${projectId}/expenses`, expense);

  // `res.headers.location` is considered as a string, even though it could be `undefined`
  // e.g. when the response from the server is an error one
  const location = res.headers.location;

  // this causes this variable to be `undefined`, and leads to errors when the function is called
  return location;
}

Expected behavior

AxiosResponseHeaders should reflect the fact that headers can be undefined.

Environment

  • Axios Version: 0.27.2
  • Adapter: N/A
  • Browser: N/A
  • Browser Version: N/A
  • Node.js Version: 16.15.1
  • OS: N/A
  • Additional Library Versions: N/A

Additional context/Screenshots

N/A

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