Skip to main content

array-type

Requires using either T[] or Array<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.

Options

type ArrayOption = 'array' | 'generic' | 'array-simple';
type Options = {
default: ArrayOption;
readonly?: ArrayOption;
};

const defaultOptions: Options = {
default: 'array',
};

The rule accepts an options object with the following properties:

  • default - sets the array type expected for mutable cases.
  • readonly - sets the array type expected for readonly arrays. If this is omitted, then the value for default will be used.

Each property can be set to one of three strings: 'array' | 'generic' | 'array-simple'.

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.

const x: Array<string> = ['a', 'b'];
const y: ReadonlyArray<string> = ['a', 'b'];

"generic"

Always use Array<T> or ReadonlyArray<T> for all array types.

const x: string[] = ['a', 'b'];
const y: readonly 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).

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'];

Combination Matrix

This matrix lists all possible option combinations and their expected results for different types of Arrays.

defaultOptionreadonlyOptionArray with simple typeArray with non simple typeReadonly array with simple typeReadonly array with non simple type
arraynumber[](Foo & Bar)[]readonly number[]readonly (Foo & Bar)[]
arrayarraynumber[](Foo & Bar)[]readonly number[]readonly (Foo & Bar)[]
arrayarray-simplenumber[](Foo & Bar)[]readonly number[]ReadonlyArray<Foo & Bar>
arraygenericnumber[](Foo & Bar)[]ReadonlyArray<number>ReadonlyArray<Foo & Bar>
array-simplenumber[]Array<Foo & Bar>readonly number[]ReadonlyArray<Foo & Bar>
array-simplearraynumber[]Array<Foo & Bar>readonly number[]readonly (Foo & Bar)[]
array-simplearray-simplenumber[]Array<Foo & Bar>readonly number[]ReadonlyArray<Foo & Bar>
array-simplegenericnumber[]Array<Foo & Bar>ReadonlyArray<number>ReadonlyArray<Foo & Bar>
genericArray<number>Array<Foo & Bar>ReadonlyArray<number>ReadonlyArray<Foo & Bar>
genericarrayArray<number>Array<Foo & Bar>readonly number[]readonly (Foo & Bar)[]
genericarray-simpleArray<number>Array<Foo & Bar>readonly number[]ReadonlyArray<Foo & Bar>
genericgenericArray<number>Array<Foo & Bar>ReadonlyArray<number>ReadonlyArray<Foo & Bar>