array-type
Require consistently using either
T[]
orArray<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.
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 Playgroundconst x: string[] = ['a', 'b'];
const y: readonly 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 Playgroundconst x: Array<string> = ['a', 'b'];
const y: ReadonlyArray<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 Playgroundconst 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'];
Open in Playground组合矩阵
该矩数组出了所有可能的选项组合以及不同类型数组的预期结果。
英:This matrix lists all possible option combinations and their expected results for different types of Arrays.
defaultOption | readonlyOption | 简单类型数组 | 非简单类型数组 | 简单类型的只读数组 | 非简单类型的只读数组 |
---|---|---|---|---|---|
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> |
何时不使用它
该规则纯粹是用于保持项目一致性的风格规则。 如果你不想为数组类型保持一致的样式,可以将其关闭。
英: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' }];