【发布时间】:2020-11-04 23:12:20
【问题描述】:
下面的代码会产生以下 eslint 错误:
@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}`)
}
}
承诺getGraphDetails 返回Promise<AxiosResponse<any>>
问题显然是response.headers['content-type'] 属性可能不存在于response 对象上。为了解决这个问题,我尝试先检查它,但这并没有消除警告:
if (
!(response && response.data && response.headers && response.headers['content-type'])
) { return '' }
感谢您为我提供的任何指导,以便更好地理解和解决此问题。
【问题讨论】:
-
这些错误只是被记录下来,不是吗?我的意思是,它们不会阻止阻止您的应用程序运行的错误
-
没错
标签: javascript typescript eslint