简体   繁体   English

收到此打字稿错误:对任何值不安全的成员访问 [key]

[英]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.

相关问题 任何值上的 eslint 错误不安全成员访问 ['content-type'] - eslint error Unsafe member access ['content-type'] on an any value @typescript-eslint/no-unsafe-assignment:任何值的不安全赋值 - @typescript-eslint/no-unsafe-assignment: Unsafe assignment of an any value UIComponent.getRouterFor 显示 TypeScript 错误:“任何”类型值的不安全返回 - UIComponent.getRouterFor shows TypeScript error: Unsafe return of an 'any' typed 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? ESLint:“any”的不安全分配和“any”类型值的不安全调用 - ESLint: Unsafe assignment of an `any` and Unsafe call of an `any` typed value 元素引用<any> .nativeElement:任何抛出“任何类型值的不安全调用”错误</any> - ElementRef<any>.nativeElement: any throwing "Unsafe call of an any typed value" error 打字稿,访问静态成员 - typescript, access static member 打字稿:无法访问继承的类构造函数中的成员值 - Typescript: can not access member value in inherited class constructor 为什么我在此React / Typescript应用程序中收到no exported member错误? - Why am I getting the no exported member error in this React/Typescript app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM