Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gprst opened this issue Jun 25, 2022 · 3 comments · Fixed by #4813
Closed

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

gprst opened this issue Jun 25, 2022 · 3 comments · Fixed by #4813

Comments

@gprst
Copy link

gprst commented Jun 25, 2022

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

@gprst gprst changed the title Axios response headers can be undefined (TS typing isn't strict enough) Axios response headers can be undefined (TS typing isn't strict enough) Jun 25, 2022
@wei-beamery
Copy link

The response headers were always available in previous versions. Is the expectation that response headers should be checked for undefined, or is this a recent breaking change?

@gprst
Copy link
Author

gprst commented Jul 27, 2022

I don't know about the history of the codebase. I just know that currently, you can get a value that'd be typed as string, although it could actually be undefined — so yes, the expectation is that response headers should be typed as string | undefined, to be more accurate.

I've created a PR for this.

@jasonsaayman
Copy link
Member

Merged the PR will release soon, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants