Skip to content

Latest commit

 

History

History
44 lines (30 loc) · 1.32 KB

no-new-array.md

File metadata and controls

44 lines (30 loc) · 1.32 KB

Disallow new Array()

💼 This rule is enabled in the ✅ recommended config.

🔧💡 This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

The ESLint built-in rule no-array-constructor enforces using an array literal instead of the Array constructor, but it still allows using the Array constructor with one argument. This rule fills that gap.

When using the Array constructor with one argument, it's not clear whether the argument is meant to be the length of the array or the only element.

This rule is fixable if the value type of the argument is known.

Fail

const length = 10;
const array = new Array(length);
const array = new Array(onlyElement);
const array = new Array(...unknownArgumentsList);

Pass

const length = 10;
const array = Array.from({length});
const array = [onlyElement];
const array = [...items];