@typescript-eslint/array-type

Require either T[] or Array<T> for array types, consistently

fixable

Also called typescript-eslint/array-type, typescript/array-type

string[] and Array<string> are the same type. A codebase that mixes the two makes readers stop and check whether the difference means something. This rule picks one. By default it's T[], and readonly T[] for read-only arrays.

--fix rewrites the type to the other form.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"@typescript-eslint/array-type" = "error" # or "warn"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"@typescript-eslint/array-type" = ["error", {
  default = "array", # "array", "generic" or "array-simple"
  # readonly = <"array", "generic" or "array-simple">,
}]

Examples#

example.ts2 problems
const names: Array<string> = [];
const ids: ReadonlyArray<number> = [1, 2];
example.tsNo problems
const names: string[] = [];
const ids: readonly number[] = [1, 2];

Options#

OptionDefaultWhat it does
default"array"The form for arrays you can change.
readonlythe value of defaultThe form for read-only arrays.

Each takes one of these values:

ValueWhat it does
"array"Require T[] and readonly T[].
"generic"Require Array<T> and ReadonlyArray<T>.
"array-simple"Require T[] when T is simple, and Array<T> when it isn't.

A simple type is a keyword such as string, a name without type arguments such as User, or an array. Unions, functions, object types, and generics aren't simple.

example.tswith { "default": "generic" }1 problem
const names: string[] = [];

With "array-simple", both lines are reported:

example.tswith { "default": "array-simple" }2 problems
const names: Array<string> = [];
const values: (string | number)[] = [];
example.tswith { "default": "array-simple" }No problems
const names: string[] = [];
const values: Array<string | number> = [];

readonly can differ from default:

example.tswith { "default": "array", "readonly": "generic" }No problems
const names: string[] = [];
const ids: ReadonlyArray<number> = [1, 2];