简体   繁体   English

任何值上的 eslint 错误不安全成员访问 ['content-type']

[英]eslint error Unsafe member access ['content-type'] on an any value

The following eslint errors are generated by the code below:以下 eslint 错误由以下代码生成:

@typescript-eslint/no-unsafe-member-access: Unsafe member access ['content-type'] on an any value. @typescript-eslint/no-unsafe-member-access:对任意值的不安全成员访问 ['content-type']。

export const getGraphPhoto = async () => {
  try {
    const response = await getGraphDetails(
      config.resources.msGraphPhoto.uri,
      config.resources.msGraphPhoto.scopes,
      { responseType: 'arraybuffer' }
    )
    if (!(response && response.data)) {
      return ''
    }
    const imageBase64 = new Buffer(response.data, 'binary').toString('base64')
    return `data:${response.headers['content-type']};base64, ${imageBase64}`
  } catch (error) {
    throw new Error(`Failed retrieving the graph photo: ${error as string}`)
  }
}

The promise getGraphDetails returns Promise<AxiosResponse<any>> promise getGraphDetails返回Promise<AxiosResponse<any>>

The issue is clearly that the property response.headers['content-type'] might not be present on the response object. To fix this I tried checking for it first but that doesn't get rid of the warning:问题显然是response object 中可能不存在属性response.headers['content-type'] 。为了解决这个问题,我尝试先检查它,但这并没有消除警告:

    if (
      !(response && response.data && response.headers && response.headers['content-type'])
    ) { return '' }

Thank you for any guidance you can give me to better understand and solve this issue.感谢您给我的任何指导,以便我更好地理解和解决这个问题。

在此处输入图像描述

I haven't used TypeScript in a while so I hope I don't make any mistakes... I think that the intention of the rule is not to prevent access to a property that does not exist but to warn of the access to a property of any object typified as any (explicitly or implicitly).我有一段时间没有使用 TypeScript 所以我希望我不会犯任何错误......我认为该规则的目的不是阻止访问不存在的属性,而是警告访问任何 object 的属性,典型化为any (显式或隐式)。 The rule doesn't take into account if there is code checking that this property exists but simply the type of the object.该规则不考虑是否存在此属性的代码检查,而只考虑 object 的类型。 If it is not warning about accessing to response.data or response.headers but only to response. headers['content-type']如果它不是关于访问response.dataresponse.headers的警告,而只是response. headers['content-type'] response. headers['content-type'] , I guess that getGraphDetails response is typed but headers property is typed as any . response. headers['content-type'] ,我猜getGraphDetails响应是键入的,但headers属性的类型是any I think you have three options:我认为你有三个选择:

  • Set the type to the response headers.将类型设置为响应标头。 I'm not sure if you could find one in an existing project but, if not, you could declare an Interface as explicit as you want to fit your needs and use it like this:我不确定您是否可以在现有项目中找到一个,但如果没有,您可以根据需要声明一个明确的接口并像这样使用它:
interface ResponseHeaders {
  'content-type': string,
  [key: string]: string, // you could set more explicit headers names or even remove the above and set just this line
}
const headers : ResponseHeaders = response.headers;
  • Disable the rule for this line or the entire file.禁用此行或整个文件的规则。

  • Ignore the warning.忽略警告。

To disable this rule on ESLINT you have to put this line on the rules section of the eslintrc.json file:要在 ESLINT 上禁用此规则,您必须将此行放在 eslintrc.json 文件的规则部分:

rules {
    "@typescript-eslint/no-unsafe-member-access": "off"
}

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

相关问题 收到此打字稿错误:对任何值不安全的成员访问 [key] - Getting this typescript error: Unsafe member access [key] on an any value ESLint:“any”的不安全分配和“any”类型值的不安全调用 - ESLint: Unsafe assignment of an `any` and Unsafe call of an `any` typed value @typescript-eslint/no-unsafe-assignment:任何值的不安全赋值 - @typescript-eslint/no-unsafe-assignment: Unsafe assignment of an any value Typescript:ESLint:任何类型值的不安全返回 @typescript-eslint/no-unsafe-return - Typescript : ESLint : Unsafe return of an any typed value @typescript-eslint/no-unsafe-return 如何处理 catch(err) 块上的 @typescript-eslint/no-unsafe-member-access 规则? - How to handle the @typescript-eslint/no-unsafe-member-access rule on catch(err) blocks? OAuth 2.0 in Node.js 访问令牌错误内容类型与 JSON 不兼容 - OAuth 2.0 in Node js Access Token Error The content-type is not JSON compatible 是否有任何基于文件扩展名计算内容类型的JavaScript库? - Is there any javascript library that calculate content-type based on file extension? JavaScript内容类型 - JavaScript content-type Status-415 Node.js中无效的内容类型指定错误 - Status-415 Invalid content-type specified error in nodejs AJAX 错误:“内容类型中没有多部分边界参数” - AJAX error: "No multipart boundary param in Content-Type"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM