javascript - 我可以使用 TypeScript 在 axios.get 方法中指定泛型类型的任何类型
问题描述
axios.get('/api')
当我像上面那样使用 TypeScript 编码时,我应该更好地指定类型,因为我可以引用 axios 的类型定义,如下所示。
(method) AxiosInstance.get<any, AxiosResponse<any>>(url: string, config?: AxiosRequestConfig | undefined): Promise<AxiosResponse<any>>
^^^ <- ???
我无法理解any
第一种通用类型的 get 方法的类型AxiosInstance.get<any,
。这个any
要干什么用?
解决方案
看看axios 类型定义。
get<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
第一个类型参数是 api 返回的类型。这默认为任何。
二是响应类型。这默认为带有第一个参数的类型的响应。
推荐阅读
- android - 对特征/描述符寻址和 UUID 感到困惑
- reactjs - 世博会相机参考未被识别
- epplus - EPPlus 条件格式“AddExpression”问题
- c++ - Lambda 自身返回:这合法吗?
- vsto - 如果选中功能区复选框,如何在发送时更改主题行
- c# - C# WPF - 模板更改后在 ListView 中找不到 ScrollContentPresenter
- c# - Unity UI 文本位置
- ruby - Fluentd Openstack 日志正则表达式格式
- go - 如何检查 golang 代码并忽略供应商库
- java - 使用 selenium 检查表中的复选框,直到表的最后一个索引页