javascript Axios typescript自定义AxiosRequestConfig

a11xaf1n  于 9个月前  发布在  Java
关注(0)|答案(2)|浏览(181)

我正在使用React和Axios。最近,我在Axios上创建了一个自定义配置,如下所示:

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

字符串
但最终的TS编译:
类型“{ handlerEnabled:boolean; }”的参数不可分配给类型“AxiosRequestConfig”的参数。对象文本只能指定已知属性,并且“handlerEnabled”在类型“AxiosRequestConfig”中不存在。
如何在AxiosRequestConfig上分配新类型?类似于axios<AxiosRequestConfig & newType>
我不想使用像.d.ts这样的旧方法。

gkn4icbw

gkn4icbw1#

通过使用typescript声明合并特性,您可以扩展任何库类型。(docs
所以这个应该可以完成任务:

// theFileYouDeclaredTheCustomConfigIn.ts
declare module 'axios' {
  export interface AxiosRequestConfig {
    handlerEnabled: boolean;
  }
}

字符串

biswetbf

biswetbf2#

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

declare module 'axios' {   
    export interface AxiosRequestConfig {
        handlerEnabled?: boolean;   
    } 
}

字符串

相关问题