Skip to content

Change headers type to string record#3021

Merged
jasonsaayman merged 2 commits intoaxios:masterfrom
remcohaszing:add-header-types
Sep 5, 2021
Merged

Change headers type to string record#3021
jasonsaayman merged 2 commits intoaxios:masterfrom
remcohaszing:add-header-types

Conversation

@remcohaszing
Copy link
Copy Markdown
Contributor

This correctly reflects the type in a more type safe manner.

@remcohaszing
Copy link
Copy Markdown
Contributor Author

@jasonsaayman Could you also have a look at this please? 😃

I’m also tagging @TimWolla because of their involvement in #2797

This forces TypeScript users to use objects whose values are strings as headers. I.e., currently the following is allowed:

import axios from 'axios';

axios.post('', {}, { headers: 'Invalid' })
axios.post('', {}, { headers: 13 })
axios.post('', {}, { headers: [] })
axios.post('', {}, { headers: { authorization: [] } })
// etc…

This changes the types, so only proper authorization headers are allowed, and the response header types are correct as well.

import axios from 'axios';

axios.post('', {}, { headers: { authorization: 'A string :)' } })

I don’t consider this to be a breaking change, as it still works the same for proper usage.

Copy link
Copy Markdown
Contributor

@TimWolla TimWolla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about the Record type.

I don’t consider this to be a breaking change, as it still works the same for proper usage.

This is technically a breaking change, but only in a regard that it will find an actual bug in a consumer's code. TypeScript users will certainly appreciate the increased strictness in typing.

This correctly reflects the type in a more type safe manner.
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 this pull request may close these issues.

4 participants