question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use Axios with TypeScript when using response interceptors (AxiosResponse issue)

See original GitHub issue

Summary

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:closed
  • Created 5 years ago
  • Reactions:82
  • Comments:50 (15 by maintainers)

github_iconTop GitHub Comments

92reactions
jeremy-wwcommented, Dec 18, 2018

I override AxiosResponse in my axios.d.ts:

import axios from 'axios'

declare module 'axios' {
  export interface AxiosResponse<T = any> extends Promise<T> {}
}
92reactions
zceicommented, May 2, 2018

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:

interface ServerResponse {
  data: ServerData
}

interface ServerData {
  foo: string
  bar: number
}

Then you can say:

axios.request<ServerData>({
  url: 'https://example.com/path/to/data',
  transformResponse: (r: ServerResponse) => r.data
}).then((response) => {
  // `response` is of type `AxiosResponse<ServerData>`
  const { data } = response
  // `data` is of type ServerData, correctly inferred
})
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found