ts axios中报 Property ‘code‘ does not exist on type ‘AxiosResponse<any, any>‘
ts语法有严格的格式,如果我们在处理响应数据时,出现了axios响应中非默认字段,就会出现标题那样的警告,我们可以通过创建axios.dt.ts解决这个问题
下面是我在开发中遇到的警告,code并不是axios默认返回的字段,按照严格的格式要求肯定会出现警告:
Axios响应中的默认字段是这样的:
export interface AxiosResponse<T = any, D = any> {data: T;status: number;statusText: string;headers: AxiosResponseHeaders;config: AxiosRequestConfig<D>;request?: any;
}
解决方法:我们只需要在src文件下新建axios.d.ts文件,并把下面内容复制进去,然后重启服务器就可以了:
import * as axios from 'axios'declare module 'axios' {interface AxiosInstance {(config: AxiosRequestConfig): Promise<any>}
}
原文链接:Property ‘code‘ does not exist on type ‘AxiosResponse<any, any>‘_property 'code' does not exist on type 'axiosrespo-CSDN博客