Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to create a function that will accept an optional parameter for an axios instance. I've tried the following, using AxiosInstance but when webpack tries to transpile the TypeScript it throws this error: TS2304: Cannot find name 'AxiosInstance'

It works, kinda, when I directly import the type declaration:

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

But this feels like a very ugly solution. Is there a better way to do this?

Example:

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) => { 
    /* ... */
  });
};

See: Axios Type Definitions

like image 475
Herlander Pinto Avatar asked Oct 19 '25 10:10

Herlander Pinto


1 Answers

It is because you are not importing AxiosInstance interface.

import axios, { AxiosInstance } from "axios";

...
like image 114
Batu G. Avatar answered Oct 21 '25 22:10

Batu G.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!