array-type
Require using either
T[]
orArray<T>
for arrays.
Using the same style for array definitions across your codebase makes it easier for your developers to read and understand the types.
Attributes
- Included in configs
- ✅ Recommended
- 🔒 Strict
- Fixable
- 🔧 Automated Fixer
- 💡 Suggestion Fixer
- 💭 Requires type information
Rule Details
This rule aims to standardize usage of array types within your codebase.
module.exports = {
"rules": {
"@typescript-eslint/array-type": "warn"
}
};
Options
This rule accepts an options object with the following properties:
interface Options {
/**
* The array type expected for mutable cases...
*/
default?: "array" | "generic" | "array-simple";
/**
* The array type expected for readonly cases. If omitted, the value for `default` will be used.
*/
readonly?: "array" | "generic" | "array-simple";
}
const defaultOptions: Options = [{ default: "array" }];
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.
- ❌ Incorrect
- ✅ Correct
const x: Array<string> = ['a', 'b'];
const y: ReadonlyArray<string> = ['a', 'b'];
const x: string[] = ['a', 'b'];
const y: readonly string[] = ['a', 'b'];
"generic"
Always use Array<T>
or ReadonlyArray<T>
for all array types.
- ❌ Incorrect
- ✅ Correct
const x: string[] = ['a', 'b'];
const y: readonly string[] = ['a', 'b'];
const x: Array<string> = ['a', 'b'];
const y: ReadonlyArray<string> = ['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<T>
or ReadonlyArray<T>
for all other types (union types, intersection types, object types, function types, etc).
- ❌ Incorrect
- ✅ Correct
const a: (string | number)[] = ['a', 'b'];
const b: { prop: string }[] = [{ prop: 'a' }];
const c: (() => void)[] = [() => {}];
const d: Array<MyType> = ['a', 'b'];
const e: Array<string> = ['a', 'b'];
const f: ReadonlyArray<string> = ['a', 'b'];
const a: Array<string | number> = ['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<Foo & Bar> |
array | generic | number[] | (Foo & Bar)[] | ReadonlyArray<number> | ReadonlyArray<Foo & Bar> |
array-simple | number[] | Array<Foo & Bar> | readonly number[] | ReadonlyArray<Foo & Bar> | |
array-simple | array | number[] | Array<Foo & Bar> | readonly number[] | readonly (Foo & Bar)[] |
array-simple | array-simple | number[] | Array<Foo & Bar> | readonly number[] | ReadonlyArray<Foo & Bar> |
array-simple | generic | number[] | Array<Foo & Bar> | ReadonlyArray<number> | ReadonlyArray<Foo & Bar> |
generic | Array<number> | Array<Foo & Bar> | ReadonlyArray<number> | ReadonlyArray<Foo & Bar> | |
generic | array | Array<number> | Array<Foo & Bar> | readonly number[] | readonly (Foo & Bar)[] |
generic | array-simple | Array<number> | Array<Foo & Bar> | readonly number[] | ReadonlyArray<Foo & Bar> |
generic | generic | Array<number> | Array<Foo & Bar> | ReadonlyArray<number> | ReadonlyArray<Foo & Bar> |