Axios typescript customize AxiosRequestConfig

冷暖自知 提交于 2020-04-16 02:18:06

问题


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


回答1:


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;
  }
}


来源:https://stackoverflow.com/questions/58777924/axios-typescript-customize-axiosrequestconfig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!