--- description: 'Require consistently using either `T[]` or `Array` for arrays.' --- import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; > 🛑 This file is source code, not the primary documentation location! 🛑 > > See **https://typescript-eslint.io/rules/array-type** for documentation. TypeScript provides two equivalent ways to define an array type: `T[]` and `Array`. The two styles are functionally equivalent. Using the same style consistently across your codebase makes it easier for developers to read and understand array types. ## Options The default config will enforce that all mutable and readonly arrays use the `'array'` syntax. ### `"array"` Always use `T[]` or `readonly T[]` for all array types. ```ts option='{ "default": "array" }' const x: Array = ['a', 'b']; const y: ReadonlyArray = ['a', 'b']; ``` ```ts option='{ "default": "array" }' const x: string[] = ['a', 'b']; const y: readonly string[] = ['a', 'b']; ``` ### `"generic"` Always use `Array`, `ReadonlyArray`, or `Readonly>` for all array types. `readonly T[]` will be modified to `ReadonlyArray` and `Readonly` will be modified to `Readonly>`. ```ts option='{ "default": "generic" }' const x: string[] = ['a', 'b']; const y: readonly string[] = ['a', 'b']; const z: Readonly = ['a', 'b']; ``` ```ts option='{ "default": "generic" }' const x: Array = ['a', 'b']; const y: ReadonlyArray = ['a', 'b']; const z: Readonly> = ['a', 'b']; ``` ### `"array-simple"` Use `T[]` or `readonly T[]` for simple types (i.e. types which are just primitive names or type references). Use `Array` or `ReadonlyArray` for all other types (union types, intersection types, object types, function types, etc). ```ts option='{ "default": "array-simple" }' const a: (string | number)[] = ['a', 'b']; const b: { prop: string }[] = [{ prop: 'a' }]; const c: (() => void)[] = [() => {}]; const d: Array = ['a', 'b']; const e: Array = ['a', 'b']; const f: ReadonlyArray = ['a', 'b']; ``` ```ts option='{ "default": "array-simple" }' const a: Array = ['a', 'b']; const b: Array<{ prop: string }> = [{ prop: 'a' }]; const c: Array<() => void> = [() => {}]; const d: MyType[] = ['a', 'b']; const e: string[] = ['a', 'b']; const f: readonly string[] = ['a', 'b']; ``` ## Combination Matrix This matrix lists all possible option combinations and their expected results for different types of Arrays. | defaultOption | readonlyOption | Array with simple type | Array with non simple type | Readonly array with simple type | Readonly array with non simple type | | -------------- | -------------- | ---------------------- | -------------------------- | ------------------------------- | ----------------------------------- | | `array` | | `number[]` | `(Foo & Bar)[]` | `readonly number[]` | `readonly (Foo & Bar)[]` | | `array` | `array` | `number[]` | `(Foo & Bar)[]` | `readonly number[]` | `readonly (Foo & Bar)[]` | | `array` | `array-simple` | `number[]` | `(Foo & Bar)[]` | `readonly number[]` | `ReadonlyArray` | | `array` | `generic` | `number[]` | `(Foo & Bar)[]` | `ReadonlyArray` | `ReadonlyArray` | | `array-simple` | | `number[]` | `Array` | `readonly number[]` | `ReadonlyArray` | | `array-simple` | `array` | `number[]` | `Array` | `readonly number[]` | `readonly (Foo & Bar)[]` | | `array-simple` | `array-simple` | `number[]` | `Array` | `readonly number[]` | `ReadonlyArray` | | `array-simple` | `generic` | `number[]` | `Array` | `ReadonlyArray` | `ReadonlyArray` | | `generic` | | `Array` | `Array` | `ReadonlyArray` | `ReadonlyArray` | | `generic` | `array` | `Array` | `Array` | `readonly number[]` | `readonly (Foo & Bar)[]` | | `generic` | `array-simple` | `Array` | `Array` | `readonly number[]` | `ReadonlyArray` | | `generic` | `generic` | `Array` | `Array` | `ReadonlyArray` | `ReadonlyArray` | ## When Not To Use It This rule is purely a stylistic rule for maintaining consistency in your project. You can turn it off if you don't want to keep a consistent style for array types. However, keep in mind that inconsistent style can harm readability in a project. We recommend picking a single option for this rule that works best for your project.