簡體   English   中英

如何在 TypeScript 中使用包含的 Axios 類型定義進行依賴注入

[英]How can I use the included Axios Type definitions for dependency injection in TypeScript

我正在嘗試創建一個函數,該函數將接受 axios 實例的可選參數。 我嘗試了以下操作,使用AxiosInstance但是當 webpack 嘗試轉換 TypeScript 時,它會拋出此錯誤: TS2304: Cannot find name 'AxiosInstance'

當我直接導入類型聲明時,它有點工作:

import { AxiosInstance } from "../../node_modules/axios/index.d";

但這感覺是一個非常丑陋的解決方案。 有沒有更好的方法來做到這一點?

示例:

import axios from "axios";

export const loadFromAPI = (
    url: string, 
    param: string, 
    id: number, 
    axiosInstance: AxiosInstance = axios.create({baseURL: url})
): Promise<any> => {
   return new Promise( (resolve: (res: any) => void, reject: (err: any) => void) => { 
    /* ... */
  });
};

請參閱: Axios 類型定義

這是因為您沒有導入AxiosInstance接口。

import axios, { AxiosInstance } from "axios";

...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM