Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios typescript customize AxiosRequestConfig

im using React and axios, recently i have created a custom config on axios like this:

import $axios from 'helpers/axiosInstance'
$axios.get('/customers', { handlerEnabled: false })

but the result ts compilation:

Argument of type '{ handlerEnabled: boolean; }' is not assignable to parameter of type 'AxiosRequestConfig'. Object literal may only specify known properties, and 'handlerEnabled' does not exist in type 'AxiosRequestConfig'.

how can i assign new types on AxiosRequestConfig? something like this axios<AxiosRequestConfig & newType>

dont wanna use old method like .d.ts

like image 338
Zeinab Malaki Avatar asked Nov 09 '19 09:11

Zeinab Malaki


2 Answers

You can extend any library types, by using the typescript decleration merging feature. (docs)

So this should do the job:

// theFileYouDeclaredTheCustomConfigIn.ts
declare module 'axios' {
  export interface AxiosRequestConfig {
    handlerEnabled: boolean;
  }
}
like image 189
Eddie Cooro Avatar answered Nov 15 '22 15:11

Eddie Cooro


// in axios.d.ts
import 'axios';

declare module 'axios' {   
    export interface AxiosRequestConfig {
        handlerEnabled?: boolean;   
    } 
}
like image 2
Mathura Das Avatar answered Nov 15 '22 16:11

Mathura Das