Axios打字稿自定义AxiosRequestConfig [英] Axios typescript customize AxiosRequestConfig

查看:2203
本文介绍了Axios打字稿自定义AxiosRequestConfig的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但结果是ts编译:

类型'{ }"不能分配给"AxiosRequestConfig"类型的参数. 对象文字只能指定已知的属性,而'AxiosRequestConfig'类型中不存在'handlerEnabled'.

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'.

如何在AxiosRequestConfig上分配新类型? 像这样的axios<AxiosRequestConfig & newType>

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

不要使用像.d.ts

推荐答案

您可以通过使用Typescript Declecle合并功能来扩展任何库类型. (文档)

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

这应该可以完成工作:

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

这篇关于Axios打字稿自定义AxiosRequestConfig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆