Object is of type 'unknown' on catch block

Viewed 1784

I'm trying to create an axios abstraction, but I'm getting this error on catch block.

Object is of type 'unknown'.

import axios, { AxiosResponse } from "axios";
import { injectable } from "inversify";
import { HttpRequest, HttpResponse, IHttp } from "../../interfaces/Ihttp";

@injectable()
export class AxiosHttpClient implements IHttp {
  async request(params: HttpRequest): Promise<HttpResponse> {
    let axiosResponse: AxiosResponse;

    try {
      axiosResponse = await axios.request({
        url: params.url,
        method: params.method,
        data: params.body,
        headers: params.headers,
      });
    } catch (error) {
      axiosResponse = error.response;
    }

    return {
      statusCode: axiosResponse.status,
      data: axiosResponse.data,
    };
  }
}
1 Answers

I solved the problem putting the useUnknownInCatchVariables option to false in tsconfig.json

Related