export const getKnowledgePage = Tparams KnowledgeParams = axiosgetany IResponseDataTUSER_APIgethome params报错解决
该报错可能是由于axios类型定义有误导致的,需要对axios的类型定义进行修正。具体做法如下:
- 找到axios的类型定义文件,一般为
index.d.ts或axios.d.ts。 - 在该文件中找到
axios函数的定义,一般为:
declare function axios<T = any>(config: AxiosRequestConfig): AxiosPromise<T>;
declare namespace axios {
// ...
}
- 修改该函数的定义,添加一个泛型参数
R,用于指定响应数据的类型:
declare function axios<T = any, R = IResponseData<T>>(
config: AxiosRequestConfig
): AxiosPromise<R>;
declare namespace axios {
// ...
}
- 在调用
axios.get时,指定泛型参数T,用于指定响应数据的类型:
export const getKnowledgePage = <T>(params: KnowledgeParams) =>
axios.get<IResponseData<T>>(USER_API.gethome, { params });
这样就可以避免报错了
原文地址: http://www.cveoy.top/t/topic/hun4 著作权归作者所有。请勿转载和采集!