@typescript-eslint/array-type
Require either T[] or Array<T> for array types, consistently
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:
[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.
[lint.rules]
"@typescript-eslint/array-type" = ["error", {
default = "array", # "array", "generic" or "array-simple"
# readonly = <"array", "generic" or "array-simple">,
}]Examples#
const names: Array<string>Array type using Array<string> is forbidden. Use string[] instead. = [];
const ids: ReadonlyArray<number>Array type using ReadonlyArray<number> is forbidden. Use readonly number[] instead. = [1, 2];const names: string[] = [];
const ids: readonly number[] = [1, 2];Options#
| Option | Default | What it does |
|---|---|---|
default | "array" | The form for arrays you can change. |
readonly | the value of default | The form for read-only arrays. |
Each takes one of these values:
| Value | What 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.
{ "default": "generic" }1 problemconst names: string[]Array type using string[] is forbidden. Use Array<string> instead. = [];With "array-simple", both lines are reported:
{ "default": "array-simple" }2 problemsconst names: Array<string>Array type using Array<string> is forbidden for simple types. Use string[] instead. = [];
const values: (string | number)[]Array type using T[] is forbidden for non-simple types. Use Array<T> instead. = [];{ "default": "array-simple" }No problemsconst names: string[] = [];
const values: Array<string | number> = [];readonly can differ from default:
{ "default": "array", "readonly": "generic" }No problemsconst names: string[] = [];
const ids: ReadonlyArray<number> = [1, 2];