How to use Axios with TypeScript when using response interceptors (AxiosResponse issue)
See original GitHub issueSummary
In a project I am migrating to TypeScript (TS), I have a response interceptor r => r.data
. How do I inform TS that I am not expecting a type of AxiosResponse
? I’ve tried overriding using as Array<...
but that doesn’t work as AxiosResponse
can’t be cast as an Array
(e.g. does not has .length).
Thanks!
Context
- axios version: 0.16.2
- Environment: Visual Studio Code
Issue Analytics
- State:
- Created 5 years ago
- Reactions:82
- Comments:50 (15 by maintainers)
Top Results From Across the Web
Setting up Axios Interceptors (React.js + TypeScript)
Using the created function to apply the interceptors Once the interceptors configuration are ready, we can use the setupInterceptorsTo() ...
Read more >How to use Axios interceptor with typescript correctly?
You have to just specify the response data type in axios get method, like so: axios.get<never, YourDataType>(...).
Read more >“How to use Axios with TypeScript when using response ...
1. // Add a response interceptor ; 2. HTTP.interceptors.response.use(function (response) { ; 3. return response ; 4. }, function(error) { ; 5. if...
Read more >How to use Axios with TypeScript when using ... - Bountysource
In a project I am migrating to TypeScript (TS), I have a response interceptor r => r.data . How do I inform TS...
Read more >Intercepting Requests & Responses Using Axios
Setting up Axios interceptors · Create a new Axios instance with a custom config · Create request, response & error handlers · Configure/make...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I override
AxiosResponse
in myaxios.d.ts
:Use the
axios.request<T>(...args)
style definition. The last Response-interceptor in the array implicitly comply to an interface like(currentResponse: any) => T
So if you have
data
being something like:Then you can say: