no-object-methods-on-collections
Disallow using Object methods on Map and Set instances.
This rule requires type information to run, which comes with performance tradeoffs.
Methods like Object.entries(), Object.keys(), and Object.values() can be
used work with collections of data stored in objects. However, when working with Map or Set objects, even though
they are collections, using these methods are a mistake because they do not properly write to (in the case of
Object.assign()) or read from the object.
This rule prevents such methods from being used on Map and Set objects.
- ❌ Incorrect
- ✅ Correct
console.log(Object.values(new Set('abc')));
Object.assign(new Map(), { k: 'v' });
Open in Playground console.log([...new Set('abc').values()]);
new Map().set('k', 'v');
Open in Playground- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-object-methods-on-collections": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-object-methods-on-collections": "error"
}
};
Try this rule in the playground ↗
Options
This rule is not configurable.
When Not To Use It
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.
See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.