joao
joao

Reputation: 451

Object is of type 'unknown' on catch block

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,
    };
  }
}

Upvotes: 2

Views: 2038

Answers (1)

joao
joao

Reputation: 451

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

Upvotes: 5

Related Questions