@typescript-eslint/array-type
Require either `T[]` or `Array<T>` for array types, consistently
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#
Reported:
const names: Array<string> = [];
const ids: ReadonlyArray<number> = [1, 2];Not reported:
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.
const names: string[] = [];With "array-simple", both lines are reported:
const names: Array<string> = [];
const values: (string | number)[] = [];const names: string[] = [];
const values: Array<string | number> = [];readonly can differ from default:
const names: string[] = [];
const ids: ReadonlyArray<number> = [1, 2];