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

查看:99
本文介绍了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编译:

'{ handlerEnabled: boolean; 类型的参数}' 不可分配给类型为 '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 &新类型>

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

不想使用像 .d.ts

推荐答案

您可以使用 typescript 声明合并功能扩展任何库类型.(docs)

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天全站免登陆