我可以使用 TypeScript 在 axios.get 方法中的泛型类型中指定任何类型

gip*_*any 1 javascript typescript typescript-generics axios

axios.get('/api')
Run Code Online (Sandbox Code Playgroud)

当我像上面那样使用 TypeScript 进行编码时,我应该更好地指定类型,因为我可以引用 axios 的类型定义,如下所示。

(method) AxiosInstance.get<any, AxiosResponse<any>>(url: string, config?: AxiosRequestConfig | undefined): Promise<AxiosResponse<any>>
                           ^^^ <- ???
Run Code Online (Sandbox Code Playgroud)

我无法理解anyget 方法的第一个泛型类型的类型AxiosInstance.get<any,。这个要用来做什么呢any

Ben*_*ies 5

查看axios 类型定义

get<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
Run Code Online (Sandbox Code Playgroud)

第一个类型参数是 api 返回的类型。这默认为任何。

第二个是响应类型。默认情况下,响应携带第一个参数的类型。