Skip to main content

array-type

Require consistently using either T[] or Array<T> for arrays.

🎨

ESLint 配置 中扩展"plugin:@typescript-eslint/stylistic" 可启用此规则。

🔧

此规则报告的一些问题可通过 --fix ESLint 命令行选项自动修复


TypeScript 提供了两种等效的方法来定义数组类型: T[]Array<T>。 这两种样式在功能上是等效的。 在代码库中一致使用相同的样式可以使开发者更轻松地阅读和理解数组类型。

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

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/array-type": "error"
}
};
在线运行试试这个规则 ↗

选项

默认配置将强制所有可变和只读数组使用 'array' 语法。

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

"array"

所有数组类型始终使用 T[]readonly T[]

英:Always use T[] or readonly T[] for all array types.

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

"generic"

所有数组类型始终使用 Array<T>ReadonlyArray<T>

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

const x: string[] = ['a', 'b'];
const y: readonly string[] = ['a', 'b'];
Open in Playground

"array-simple"

对于简单类型(即只是原始名称或类型引用的类型),请使用 T[]readonly T[]。 对所有其他类型(联合类型、交集类型、对象类型、函数类型等)使用 Array<T>ReadonlyArray<T>

英: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'];
Open in Playground

组合矩阵

该矩数组出了所有可能的选项组合以及不同类型数组的预期结果。

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

defaultOptionreadonlyOption简单类型数组非简单类型数组简单类型的只读数组非简单类型的只读数组
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>

何时不使用它

该规则纯粹是用于保持项目一致性的风格规则。 如果你不想为数组类型保持一致的样式,可以将其关闭。

英: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.

选项

该规则接受以下选项

type ArrayOption = 'array' | 'array-simple' | 'generic';

type Options = [
{
/** 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' }];

资源