简体   繁体   中英

Unsafe use of expression any on header object

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'.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM