0

So i'm having the no-unsafe-any linting error on Typescript when trying to get a custom header from my header variable.

It is defined in this interface:

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

and when trying to get a custom id am getting this error like this:

    case HttpStatusCode.SeeOther: {
                const errorMessage: string =
                    (err.response.data as GenericError).message ||
                    err.response.statusText;
                if (err.response.headers.id && event) {
                    apiResponse = ResponseBuilder.seeOther(
                        requestContext,
                        HttpStatusCode.SeeOther,
                        errorMessage,
                        {
                            location: `${event.requestContext.resourcePath}/${err.response.headers.id}`,
                        }
                    );

I have tried several ways but i cannot make it work.

ERROR: (no-unsafe-any) utilities.ts[159, 40]: Unsafe use of expression of type 'any'. ERROR: (no-unsafe-any) utilities.ts[159, 41]: Unsafe use of expression of type 'any'.

JustAJavaUser
  • 89
  • 2
  • 9

1 Answers1

1

The no-unsafe-any is meant to warn you when you try to access something typed to any. You can get around this by correctly typing before using like so:

err.response.headers.id // this will error 
(err.response.headers as unknown as {id: string}).id // this should work

g2jose
  • 1,367
  • 1
  • 9
  • 11