💼 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.
const length = 10;
const array = new Array(length);
const array = new Array(onlyElement);
const array = new Array(...unknownArgumentsList);
const length = 10;
const array = Array.from({length});
const array = [onlyElement];
const array = [...items];