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

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

但结果ts编译:



如何在AxiosRequestConfig上分配新类型?
像这样的axios<AxiosRequestConfig & newType>
不想使用像.d.ts这样的旧方法

最佳答案

您可以使用 typescript 清理合并功能来扩展任何库类型。 (docs)

因此,这应该可以完成工作:

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

09-26 20:45