arrayConstructors
Reports using the
Arrayconstructor 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.
Examples
Section titled “Examples”const values = new Array();const values = new Array(1, 2, 3);const values = Array();const values = Array("a", "b", "c");const values: number[] = [];const values = [1, 2, 3];const sparse = new Array(10);const typed = new Array<number>();const typed = Array<string>(1, 2, 3);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.