[英]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.data
或response.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:我认为你有三个选择:
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.