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
It is because you are not importing AxiosInstance interface.
import axios, { AxiosInstance } from "axios";
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With