Skip to content

arrayConstructors

Reports using the Array constructor to create arrays instead of array literal syntax.

✅ This rule is included in the ts logical preset.

The Array constructor has confusing behavior when called with a single numeric argument. Instead of creating an array containing that number, it creates a sparse array of that length. Array literal syntax is clearer and avoids this pitfall.

This rule allows new Array(n) with a single numeric argument for intentionally creating sparse arrays, and allows Array<T>() with type arguments for creating typed arrays.

const values = new Array();
const values = new Array(1, 2, 3);
const values = Array();
const values = Array("a", "b", "c");

This rule is not configurable.

If you prefer the Array constructor style for readability or consistency reasons, you may disable this rule. Some codebases may have established patterns using the constructor syntax.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.