Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

array-type

Require consistently using eitherT[] orArray<T> for arrays.

🔧

Some problems reported by this rule are automatically fixable by the--fix ESLint command line option.

TypeScript provides two equivalent ways to define an array type:T[] andArray<T>.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.

  • Flat Config
  • Legacy Config
eslint.config.mjs
exportdefault tseslint.config({
rules:{
"@typescript-eslint/array-type":"error"
}
});

Try this rule in the playground ↗

Options

This rule accepts the following options:

typeArrayOption='array'|'array-simple'|'generic';

typeOptions=[
{
/** The array type expected for mutable cases. */
default?: ArrayOption;
/** The array type expected for readonly cases. If omitted, the value for `default` will be used. */
readonly?: ArrayOption;
},
];

const defaultOptions: Options=[{default:'array'}];

The default config will enforce that all mutable and readonly arrays use the'array' syntax.

"array"

Always useT[] orreadonly T[] for all array types.

  • ❌ Incorrect
  • ✅ Correct
const x:Array<string>=['a','b'];
const y: ReadonlyArray<string>=['a','b'];
Open in Playground

"generic"

Always useArray<T>,ReadonlyArray<T>, orReadonly<Array<T>> for all array types.readonly T[] will be modified toReadonlyArray<T> andReadonly<T[]> will be modified toReadonly<Array<T>>.

  • ❌ Incorrect
  • ✅ Correct
const x:string[]=['a','b'];
const y:readonlystring[]=['a','b'];
const z: Readonly<string[]>=['a','b'];
Open in Playground

"array-simple"

UseT[] orreadonly T[] for simple types (i.e. types which are just primitive names or type references).UseArray<T> orReadonlyArray<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'];
Open in Playground

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>

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.

Resources


[8]ページ先頭

©2009-2025 Movatter.jp