no-array-constructor-elements
Disallows new Array(...) forms that read like element lists and catches a few array initialization patterns that are
easy to rewrite.
Rule details Problem
Section titled “Rule details ”ProblemBy default, this rule runs in roblox-ts mode and requires a type for new Array() with no arguments.
It reports these cases:
new Array("a", "b")and similar enumeration forms, usually fixed to[...]new Array("a"), fixed to["a"]new Array()without type arguments or contextualArray<T>/ReadonlyArray<T>typingconst items = new Array<T>(); items.push(...)when the pushes are immediate and consecutive
In standard mode, new Array(length) is also reported and gets an Array.from({ length }) suggestion.
In roblox-ts mode, length-style constructors stay allowed. The rule also leaves alone roblox-ts overloads such as
new Array(length, fill) when the first argument could be numeric.
Configuration
Section titled “Configuration”import ceaseNonsense from "eslint-plugin-cease-nonsense";
export default [ { plugins: { "cease-nonsense": ceaseNonsense, }, rules: { "cease-nonsense/no-array-constructor-elements": ["error", { "environment": "standard", "requireExplicitGenericOnNewArray": true }], }, },];Options
Section titled “Options”| Option | Default | What it does |
|---|---|---|
environment | "roblox-ts" | "roblox-ts" allows length-style new Array(...) overloads, "standard" reports new Array(length) |
requireExplicitGenericOnNewArray | true | Requires new Array<T>() or matching contextual array typing for zero-argument calls |
Examples
Section titled “Examples” Incorrect
const letters = new Array("a", "b");const single = new Array("a");const missingType = new Array();
const items = new Array<string>();items.push("a");items.push("b"); Correct
const letters = ["a", "b"];const single = ["a"];const typed = new Array<string>();const contextual: Array<string> = new Array();const sized = new Array(10); // valid in roblox-ts modePush collapse behavior
Section titled “Push collapse behavior”The new Array<T>() plus .push(...) rewrite only happens when all of these are true:
- the declaration is a single
constorlet - the
pushcalls are immediate and consecutive in the same block - there is no later
pushcall on the same variable in that block or program section - every pushed value is safe to inline, otherwise the rule gives a suggestion instead of an automatic fix
Related rules
Section titled “Related rules” no-empty-array-literal Checks empty array literal usage
no-array-size-assignment Replaces append-by-size indexing with `push`