Skip to content

No Array Constructor Index Assignment

SuggestionAuto-fixableRoblox
small-rules/no-array-constructor-index-assignment

Disallow new Array<T>() followed by contiguous index assignments; use an array literal instead.

Rationale

new Array<T>() then immediately writing arr[0] = a; arr[1] = b; right after is just a slow, ugly way to write [a, b]. And on roblox-ts specifically, you’re doing something that Luau does not like: unnecessary indexing.

This catches the pattern where you declare an empty array and fill it in by index right after. Auto-fixes to a literal when the assignments are contiguous and don’t reference the array itself. If you’re actually building a sparse array with gaps, this rule stays out of your way.

Diagnostic Messages

preferArrayLiteral
Use an array literal instead of new Array<T>() followed by index assignments.

Configuration

This rule does not accept options.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/no-array-constructor-index-assignment": "error"
}
}

Examples

array index assignment after construction
const samples = new Array<string>();
samples[0] = replacement;

After auto-fix

const samples = [replacement];
array literal initialization
const samples = [replacement];