-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Rule proposal: Prevent Object.values(...)
usage with Map
#6807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This presents an interesting intersection with |
Heh this request reminded me of that exact same issue! Yeah there's something interesting here because they're all related but also distinct problem sets. Maybe a big rule like |
I'm somewhat interested in picking this up again since it yet-again managed to get into production 🙄 Any tips on how to identity the Initially just capturing |
I'd like to throw out another potential case of a misused // fail
Object.values(['a', 'b', 'c']) // simplify to: ['a', 'b', 'c']
const array = ['a', 'b', 'c']
Object.values(array) // simplify to: array
// pass
type MyObject = { a: string }
let arrayOrObject: array | MyObject
arrayOrObject = ['a', 'b', 'c']
Object.values(arrayOrObject) // could be an object, can't simplify |
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: