Property ‘code‘ does not exist on type ‘AxiosResponse<any, any>‘ 的解决办法

原文链接 : Property ‘xxx’ does not exist on type ‘AxiosResponse<any, any>’ 的解决办法

vue3 + ts 中 调用接口时:

const loginOut = () => {
    loginOutApi().then(res => {
        const { code } = res
        if(code == 0){ }
    })
  }

报了如下错误:
Property ‘code’ does not exist on type ‘AxiosResponse<any, any>’.ts(2339)


解决方法:

新增一个 axios.d.ts 文件,内容如下:

// 定义接口返回数据类型结构,这里根据后端返回去定义

interface resPage {
page: number
pageSize: number
total: number
hasNextPage: boolean
pageTotal: number
}

interface itfRes {
code: number
message: string | undefined
data: any
reason?: string
page?: resPage
}

import * as axios from ‘axios’

declare module ‘axios’ {
interface AxiosResponse extends itfRes
}

加上之后的效果:

猜你喜欢

转载自blog.csdn.net/qq_41356250/article/details/130358036
今日推荐