1 | # Disallow generic `Array` constructors (no-array-constructor)
|
2 |
|
3 | Use of the `Array` constructor to construct a new array is generally discouraged in favor of array literal notation because of the single-argument pitfall and because the `Array` global may be redefined. Two exceptions are when the Array constructor is used to intentionally create sparse arrays of a specified size by giving the constructor a single numeric argument, or when using TypeScript type parameters to specify the type of the items of the array (`new Array<Foo>()`).
|
4 |
|
5 | ## Rule Details
|
6 |
|
7 | This rule disallows `Array` constructors.
|
8 |
|
9 | Examples of **incorrect** code for this rule:
|
10 |
|
11 | ```ts
|
12 | /*eslint no-array-constructor: "error"*/
|
13 |
|
14 | Array(0, 1, 2)
|
15 | new Array(0, 1, 2)
|
16 | ```
|
17 |
|
18 | Examples of **correct** code for this rule:
|
19 |
|
20 | ```ts
|
21 | /*eslint no-array-constructor: "error"*/
|
22 |
|
23 | Array<number>(0, 1, 2)
|
24 | new Array<Foo>(x, y, z)
|
25 |
|
26 | Array(500)
|
27 | new Array(someOtherArray.length)
|
28 | ```
|
29 |
|
30 | ## When Not To Use It
|
31 |
|
32 | This rule enforces a nearly universal stylistic concern. That being said, this rule may be disabled if the constructor style is preferred.
|
33 |
|
34 | <sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/7685fed33b15763ee3cf7dbe1facfc5ba85173f3/docs/rules/no-array-constructor.md)</sup>
|