首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TS错误:键入“AxiosResponse<IChat,any>”不能指定键入“IChat”

TS错误:键入“AxiosResponse<IChat,any>”不能指定键入“IChat”
EN

Stack Overflow用户
提问于 2022-09-03 13:59:21
回答 1查看 68关注 0票数 0

我用方法上了课:

代码语言:javascript
复制
export default class ChatService {
    static async findDMChat(companionID: number)  {
        return $api.post<IChat>('/findDMChat', {companion_id: companionID})
    }
}

在ChatStore.setCompanion( )中,我希望将该方法的结果分配给属性:

代码语言:javascript
复制
export class ChatStore {

    companion = {} as IUser;
    chat = {} as IChat;

    constructor() {
        makeAutoObservable(this)
    }
    
    async setCompanion(companion: IUser) {
        this.companion = companion;
        this.chat = await ChatService.findDMChat(companion.id) // Type 'AxiosResponse<IChat, any>' is missing the following properties from type 'IChat': type, idts(2739)
    }

代码注释中显示了错误的描述。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-03 14:08:13

await ChatService.findDMChat(companion.id)返回响应的结果,而不仅仅是数据。如果您查看AxiosResponse类型,您将看到响应包括什么

代码语言:javascript
复制
export interface AxiosResponse<T = any, D = any>  {
  data: T;
  status: number;
  statusText: string;
  headers: AxiosResponseHeaders;
  config: AxiosRequestConfig<D>;
  request?: any;
}

因此,在您的示例中,您应该从响应中获得data属性

代码语言:javascript
复制
const { data} = await ChatService.findDMChat(companion.id);
this.chat = data;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73592798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档