简体   繁体   English

使用 Typescript 时如何避免触发 eslint 的 for..in

[英]How to avoid triggering eslint's for..in when using Typescript

I currently use the TypeScript recommended approach to iterate over object's keys., like this:我目前使用 TypeScript 推荐的方法来迭代对象的键。像这样:

    let key: keyof ExampleObject;
    for (key in exampleObject) {
      // do some work with exampleObject[key];
    }

In my current project Eslint complains about using for..in construct like this:在我当前的项目中,Eslint 抱怨使用for..in构造如下:

for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array      no-restricted-syntax
The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype                                                                 guard-for-in

I have seen several similar questions and answers for JavaScript eg using Object.keys(...) but TypeScript doesn't like this approach too and it loses some of the typesafety.我已经看到 JavaScript 的几个类似问题和答案,例如使用 Object.keys(...) 但 TypeScript 也不喜欢这种方法,它失去了一些类型安全性。

Is there a way to avoid triggering Eslint rules while still adhering to the TypeScript best practices?有没有办法避免触发 Eslint 规则,同时仍然遵守 TypeScript 最佳实践? I know I can just turn off the designated rules but I hope there is a less 'hacky' way我知道我可以关闭指定的规则,但我希望有一种不那么“hacky”的方式

Eslint is complaining for an extremely valid reason, and you should basically never use for...in unless you explicitly want to walk the prototype chain, which you basically never do. Eslint 的抱怨有一个非常正当的理由,你基本上不应该使用for...in除非你明确想要遍历原型链,而你基本上从来没有这样做过。

As for the rest...至于rest……

TypeScript doesn't like this approach too and it loses some of the typesafety. TypeScript 也不喜欢这种方法,它失去了一些类型安全性。

Yeah.是的。 Your problem isn't Object.keys , your problem is that runtime reflection and compile-time type safety are natural enemies in the wild.你的问题不是Object.keys ,你的问题是运行时反射和编译时类型安全是天敌。 So there isn't a 'clean' way to do this, you are saying "give me all of the properties of this object at runtime" which can't be safely known at compile-time, and changing the mechanism by which you ask for that isn't going to fix the fundamental underlying issue.所以没有一个“干净”的方法来做到这一点,你说“在运行时给我这个 object 的所有属性”,这在编译时不能安全地知道,并改变你问的机制因为那不会解决根本的潜在问题。

You have a few options for dealing with this.您有几种选择来处理这个问题。

  1. Change your approach.改变你的方法。 Do you really need to iterate over all the keys of that object?真的需要遍历 object 的所有键吗? All of them?全部 Or can you use a compile-time const array of known keys?或者您可以使用已知键的编译时 const 数组吗?
  2. Use compile-time legible runtime checks like instanceof , typeof , the in operator, etc to narrow the type.使用编译时清晰的运行时检查,如instanceoftypeofin运算符等来缩小类型。
  3. Accept the loss of type-safety and use casts to satisfy the compiler.接受类型安全的损失并使用强制转换来满足编译器。

暂无
暂无

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

相关问题 使用for..in的对象的Typescript复制属性 - Typescript copy properties of an object using for..in 与useEffect一起使用时如何防止触发useCallback(并遵守eslint-plugin-react-hooks)? - How to prevent useCallback from triggering when using with useEffect (and comply with eslint-plugin-react-hooks)? 如何修复 eslint 错误,即在实际使用 TypeScript props 接口时未使用它? - How can I fix the eslint error saying a TypeScript props interface is not used when it's actually used? 在最近的 eslint 中使用 Enums 时,如何避免阴影变量警告? - How do I avoid shadowed variable warnings when using Enums in recent eslint? Typescript ESLint 错误 - 使用 React 功能组件时如何输入子项? - Typescript ESLint Error - How can I type children when using react functional components? 使用`useFormikContent()`时如何防止`@typescript-eslint/unbound-method`错误? - How do I prevent a `@typescript-eslint/unbound-method` error when using `useFormikContent()`? 在 TypeScript 中使用可选泛型类型时如何避免不必要的 arguments? - How to avoid unnecessary arguments when using optional generic type in TypeScript? 使用TypeScript时如何避免两次编写Mongoose Schema? - How to avoid writing the Schema of Mongoose twice when using it with TypeScript? 使用 babel 模块解析器和 typescript 时的 ESLint 问题 - ESLint issue when using babel module-resolver and typescript 在 Typescript 和 Eslint 中使用别名时找不到模块 - cannot find module when using alias with Typescript and Eslint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM