Struggling with TypeScript in React and encountered an issue.
I decided to use a generic to build an abstracted class related to Axios.
However, I ran into an ESLint error when using any
as the type parameter for my generic.
ESLint: Unexpected any. Specify a different type.(@typescript-eslint/no-explicit-any)
ESLint suggested replacing any
with the never
type in the generic definition.
https://i.stack.imgur.com/B38xa.png
I was familiar with never
being used to indicate a lack of parameters or return values. Can it really replace any
in generics?
source
import axios, { AxiosRequestConfig, AxiosInstance, AxiosResponse } from 'axios';
import { stringify } from 'qs';
import { FilterParams } from '@src/interfaces/services/type';
type MethodType = <T = any, R = AxiosResponse<T>, D = any>(
url: string,
data?: D | undefined,
config?: AxiosRequestConfig<D>,
) => Promise<R>;
class AxiosClient {
options: AxiosRequestConfig;
client: AxiosInstance;
get: MethodType;
post: MethodType;
patch: MethodType;
delete: MethodType;
constructor() {
this.options = {
baseURL: process.env.API_ROOT || '/api/',
withCredentials: true,
paramsSerializer: (params: FilterParams) =>
stringify(params, { arrayFormat: 'repeat' }),
};
this.client = axios.create(this.options);
/* method redefinition */
this.get = this.client.get;
this.post = this.client.post;
this.patch = this.client.patch;
this.delete = this.client.delete;
}
}
export default new AxiosClient();