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博客

相关推荐
kyriewen10 小时前
产品经理把PRD写成“天书”,我用AI半小时重写了一遍,他当场愣住
前端·ai编程·cursor
humcomm10 小时前
元框架的工作原理详解
前端·前端框架
canonical_entropy11 小时前
Attractor Before Harness: AI 大规模开发的方法论
前端·aigc·ai编程
zhangxingchao11 小时前
多 Agent 架构到底怎么选?从 Claude Agent Teams、Cognition/Devin 到工程落地原则
前端·人工智能·后端
IT_陈寒11 小时前
SpringBoot那个自动配置的坑,害我排查到凌晨三点
前端·人工智能·后端
Honor丶Onlyou11 小时前
VS Code 右键菜单修复记录
前端
PILIPALAPENG11 小时前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
JYeontu11 小时前
轮播图不够惊艳?试下这个立体卡片轮播图
前端·javascript·css
张就是我10659211 小时前
从前端角度理解 CVE-2026-31431
前端
AGoodrMe11 小时前
swift基础之async/await
前端·ios