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

相关推荐
excel1 小时前
为什么 super 中能接收到 resolve 和 reject?
前端
Flynt3 小时前
AI 生成的界面,能放心点吗?我把 Google A2UI 的三层防线拆开测了一遍
前端·google·agent
前端混子4 小时前
我用 AI 做了个「禾月小历」,一套代码跑微信小程序和 H5
前端
IT_陈寒4 小时前
Java实战技巧总结:当你的finally块没有执行时会发生什么?
前端·人工智能·后端
小帅不太帅4 小时前
一年一次黑五,为了买软件真省钱,我做了个开源小站
前端·人工智能·github
吴名氏.5 小时前
从零手写一个 AI Agent 框架(TypeScript 完整实现):Function Calling 循环、流式输出与错误恢复
人工智能·typescript·node.js·大模型·ai agent·functioncalling·llm 应用开发
DKPT6 小时前
AI广场 · 从零到上线完整部署教程
vue.js
JavaGuide6 小时前
刚刚,阿里版 Codex 上线了匿名模型 HungerG,无限 Token!
前端·后端
zhanlang02176 小时前
HTML 零基础入门到企业实战全套教程【第九章 9.3】移动端 HTML 基础适配入门
前端·html
洋洋技术笔记6 小时前
过渡与动画效果
vue.js