src/content/docs/linter/rules/use-array-literals.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.7.2` - Diagnostic Category: [`lint/style/useArrayLiterals`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-array-constructor`](https://eslint.org/docs/latest/rules/no-array-constructor) - Same as [`@typescript-eslint/no-array-constructor`](https://typescript-eslint.io/rules/no-array-constructor){
"linter": {
"rules": {
"style": {
"useArrayLiterals": "error"
}
}
}
}
Disallow Array constructors.
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. The exception is when the Array constructor intentionally creates sparse arrays of a specified size by giving the constructor a single numeric argument.
const xs = Array();
const xs = Array(0, 1, 2);
const xs = new Array(0, 1, 2);
const xs = Array(...args);
const xs = new Array<number>()
const xs = Array(65000);
const xs = [0, 1, 2];