Skip to content

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.

By 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 contextual Array<T> / ReadonlyArray<T> typing
  • const 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.

eslint.config.ts
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
}],
},
},
];
OptionDefaultWhat it does
environment"roblox-ts""roblox-ts" allows length-style new Array(...) overloads, "standard" reports new Array(length)
requireExplicitGenericOnNewArraytrueRequires new Array<T>() or matching contextual array typing for zero-argument calls
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 mode

The new Array<T>() plus .push(...) rewrite only happens when all of these are true:

  • the declaration is a single const or let
  • the push calls are immediate and consecutive in the same block
  • there is no later push call 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