Skip to content

No Array Constructor Elements

ErrorAuto-fixableRoblox
small-rules/no-array-constructor-elements

Disallow array constructor element forms and enforce roblox-ts-aware constructor patterns.

Rationale

Heads up: new Array(n) is fine here. In roblox-ts it compiles to table.create(n), which is a normal, welcomed pattern. This rule does NOT flag that.

What it does catch: new Array(a, b, c) (element enumeration — just write [a, b, c]), a bare new Array() with no generic type, and the “declare an empty array then .push() a bunch right after” pattern, which it collapses into one literal. Auto-fixes when the values are side-effect free.

In standard mode it also bans new Array(n) length constructors, but in roblox-ts that’s allowed. Flip environment to "standard" if you want the stricter behavior.

Diagnostic Messages

avoidConstructorEnumeration
Do not use Array constructor enumeration arguments. Use an array literal instead.
avoidLengthConstructorInStandard
Length-based Array constructor is not allowed in standard mode. Prefer Array.from({ length: n }).
avoidSingleArgumentConstructor
Single-argument Array constructor form is not allowed here. Use an array literal instead.
collapseArrayPushInitialization
Collapse new Array<T>() + consecutive .push(...) calls into a single array literal initializer.
requireExplicitGenericOnNewArray
new Array() must use an explicit generic argument or a contextual Array<T>/ReadonlyArray<T> annotation.
suggestArrayFromLength
Replace with Array.from({ length: value }).
suggestArrayLiteral
Replace constructor form with an array literal.
suggestCollapseArrayPushInitialization
Collapse constructor + push sequence into a single array literal initializer.

Configuration

This rule accepts one options object after the severity.

environmentOptional

Array constructor environment mode: 'roblox-ts' allows new Array(length); 'standard' reports it.

requireExplicitGenericOnNewArrayOptional

When true, zero-argument new Array() requires explicit generic type arguments or contextual array typing.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/no-array-constructor-elements": [
"error",
{
"environment": "roblox-ts",
"requireExplicitGenericOnNewArray": true
}
]
}
}

Examples

array constructor with elements
const values = new Array("a", "b");

After auto-fix

const values = ["a", "b"];
empty generic array constructor
const value = new Array<string>();