UNPKG

1.25 kBMarkdownView Raw
1# Disallow generic `Array` constructors (no-array-constructor)
2
3Use 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
7This rule disallows `Array` constructors.
8
9Examples of **incorrect** code for this rule:
10
11```ts
12/*eslint no-array-constructor: "error"*/
13
14Array(0, 1, 2)
15new Array(0, 1, 2)
16```
17
18Examples of **correct** code for this rule:
19
20```ts
21/*eslint no-array-constructor: "error"*/
22
23Array<number>(0, 1, 2)
24new Array<Foo>(x, y, z)
25
26Array(500)
27new Array(someOtherArray.length)
28```
29
30## When Not To Use It
31
32This 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>