[英]Getting this typescript error: Unsafe member access [key] on an any value
I have written a deep equality check function.我写了一个深度相等检查函数。 I am getting this error for highlighted lines.
对于突出显示的行,我收到此错误。
Can anyone suggest what I need to do to fix this error (or at least what the error means).任何人都可以建议我需要做什么来修复此错误(或至少该错误意味着什么)。
Code:代码:
export const deepEqual = (object1: any, object2: any) => {
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);
if (keys1.length !== keys2.length) {
return false;
}
for (const key of keys1) {
const val1: unknown = object1[key] as unknown; // Error for this line
const val2: unknown = object2[key] as unknown; // Error for this line
const areObjects = isObject(val1) && isObject(val2);
if (
(areObjects && !deepEqual(val1, val2)) ||
(!areObjects && val1 !== val2)
) {
return false;
}
}
return true;
};
Error:错误:
45:27 error Unsafe member access [key] on an any value @typescript-eslint/no-unsafe-member-access
45:27 error Unsafe member access [key] on an any value @typescript-eslint/no-unsafe-member-access
The errors mean, that you try to access a property (or member) of an object with type any
.错误意味着,您尝试访问类型为
any
的对象的属性(或成员)。 any
in TS gives you no clues or knowledge about the values behind the variables. TS 中的
any
不提供有关变量背后值的任何线索或知识。
ESLint displays an error, since accessing members of objects with type any
can lead to unwanted (and maybe undetected) bugs. ESLint 显示错误,因为访问类型为
any
的对象成员可能会导致不需要的(并且可能未被检测到的)错误。 Your code will still compile, since TypeScript on its own has no problems with that.你的代码仍然可以编译,因为 TypeScript 本身没有问题。 If you really know what you are doing, you can disable ESLint for certain lines by commenting
如果你真的知道你在做什么,你可以通过注释禁用某些行的 ESLint
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
above them.在他们上面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.