Skip to content

Array Type Generic

ErrorAuto-fixable
small-rules/array-type-generic

Disallow bracket array type syntax and require Array<T> / ReadonlyArray<T>.

Rationale

Array&lt;T&gt; and ReadonlyArray&lt;T&gt; read like every other generic type in the codebase. The bracket form doesn’t: you have to stop and work out what readonly string[][] means, and as soon as the element type is a union or a function you need parentheses. Forget them and A | B[] means something else.

The fix rewrites nested arrays from the outside in, so readonly string[][] becomes ReadonlyArray&lt;Array&lt;string&gt;&gt;. The rule leaves arrays written inside a tuple type alone.

Diagnostic Messages

useGenericArrayType
Bracket array type syntax is not allowed. Use Array<T> or ReadonlyArray<T> generic syntax.

Configuration

This rule does not accept options.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/array-type-generic": "error"
}
}

Examples

Bracket array type syntax
type A = string[];

After auto-fix

type A = Array<string>;
Generic array type syntax
type Point = [x: number, y: number];