首页 > 解决方案 > 带有 lambda 的 Typescript 装饰器接口。AxiosInstance

问题描述

我遇到了关于 index.d.ts 文件中暴露的 lambda 函数的打字稿问题。我想要做的是装饰 AxiosInstance 以便在我想要的时候取消挂起的请求。

这是来自 axios 库的 index.d.ts 文件:

export interface AxiosInstance {

  (config: AxiosRequestConfig): AxiosPromise; // How to override this lambda ?
  (url: string, config?: AxiosRequestConfig): AxiosPromise; // How to override this lambda ?

  // Omit the rest because ther is no problem with it
}

这是我对 AxiosInstance 的实现

export default class CancelableAxiosInstance implements AxiosInstance {


    private axiosInstance: AxiosInstance;

    constructor(axiosInstance: AxiosInstance){
        this.axiosInstance = axiosInstance;
    }

    // Another function that I want to expose on CancelableAxiosInstance
    cancelRequests() {
       // Omit implementation
    }

    // How do I override (config: AxiosRequestConfig): AxiosPromise; declared in index.d.ts file ?
 }

这是 IDE 中显示的我的打字稿编译器错误:

TS2420:“CancelableAxiosInstance”类错误地实现了“AxiosInstance”接口。类型“CancelableAxiosInstance”不匹配签名“(config: AxiosRequestConfig): AxiosPromise”。

任何人都可以帮助我吗?

非常感谢

标签: typescriptaxiosdecorator

解决方案


推荐阅读