Home > OS >  TS ERROR: Type 'AxiosResponse<IChat, any>' is not assignable to type 'IChat
TS ERROR: Type 'AxiosResponse<IChat, any>' is not assignable to type 'IChat

Time:09-04

I have class with method:

export default class ChatService {
    static async findDMChat(companionID: number)  {
        return $api.post<IChat>('/findDMChat', {companion_id: companionID})
    }
}

In ChatStore.setCompanion( ) I want to assign result of that method to property:

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)
    }

Description of the error is indicated in the code comment. Thanks in advance.

CodePudding user response:

await ChatService.findDMChat(companion.id) return the result of response, not just data. If you look at AxiosResponse type you'll see what response includes

export interface AxiosResponse<T = any, D = any>  {
  data: T;
  status: number;
  statusText: string;
  headers: AxiosResponseHeaders;
  config: AxiosRequestConfig<D>;
  request?: any;
}

so, in your case you should get data property from response

const { data} = await ChatService.findDMChat(companion.id);
this.chat = data;
  • Related