no-array-constructor
Disallow Array constructors in favor of array literals
fixable
Array() behaves differently depending on how many arguments it gets. Array(10, 20) is an array with two items, but Array(10) is an array with 10 empty slots. An array literal always holds what you wrote.
The rule reports Array() and new Array() with no argument or with more than one. A call with one argument is allowed, because that's how you make an array of a given length.
--fix rewrites the call to an array literal.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"no-array-constructor" = "error" # or "warn"Examples#
example.js2 problems
const empty = new Array()The array literal notation [] is preferable.;
const sizes = Array(10, 20, 30)The array literal notation [] is preferable.;example.jsNo problems
const empty = [];
const sizes = [10, 20, 30];
const slots = new Array(5);In TypeScript, a call with a type argument isn't reported either:
example.tsNo problems
const names = new Array<string>();