简体   繁体   English

在 header object 上不安全地使用表达式 any

[英]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.因此,当我尝试从我的 header 变量中获取自定义 header 时,我在 Typescript 上遇到了 no-unsafe-any linting 错误。

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:并且在尝试获取自定义 ID 时出现如下错误:

    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'.错误: (no-unsafe-any) utilities.ts[159, 40]: 不安全地使用“any”类型的表达式。 ERROR: (no-unsafe-any) utilities.ts[159, 41]: Unsafe use of expression of type 'any'.错误: (no-unsafe-any) utilities.ts[159, 41]: 不安全地使用“any”类型的表达式。

The no-unsafe-any is meant to warn you when you try to access something typed to any .no-unsafe-any是为了在您尝试访问类型为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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