Rule proposal: Prevent Object.values(...)
usage with Map
#6807
Labels
accepting prs
Go ahead, send a pull request that resolves this issue
enhancement: new plugin rule
New rule request for eslint-plugin
package: eslint-plugin
Issues related to @typescript-eslint/eslint-plugin
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Description
Using
Object.values(...)
,Object.keys(...)
,Object.entries(...)
with aMap
object is a foot-gun because it will return empty[]
regardless of the map contents and is most likely a mistake. These sorts of mistakes are easy to get into during refactors. This issue is spawning from a real-life refactor mistake -> element-hq/element-web#24995 (comment)Related StackOverflow question: https://stackoverflow.com/questions/72315392/how-to-prevent-the-mistake-of-calling-object-values-on-a-map
As @bergus mentions from the SO question, it's possible to extend
Map
to make a subclass that returns something with the object utilities but this seems a lot more niche compared to the big problem of running them on baseMap
objects. AFAICT, it's not possible to use TypeScript on its own to catch this sort of thing because practically everything inherits fromObject
and function arguments in TypeScript are contravariant (accepts supertypes but doesn't accept subtypes).Fail Cases
Pass Cases
Additional Info
No response
The text was updated successfully, but these errors were encountered: