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.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/array-type": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/array-type": "error"
}
};
在线运行试试这个规则 ↗
选项
该规则接受以下选项:
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' }];
¥Options
默认配置将强制所有可变和只读数组使用 '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.
- ❌ Incorrect
- ✅ Correct
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>
或 Readonly<Array<T>>
。readonly T[]
将被修改为 ReadonlyArray<T>
,Readonly<T[]>
将被修改为 Readonly<Array<T>>
。
¥Always use Array<T>
, ReadonlyArray<T>
, or Readonly<Array<T>>
for all array types.
readonly T[]
will be modified to ReadonlyArray<T>
and Readonly<T[]>
will be modified to Readonly<Array<T>>
.
- ❌ Incorrect
- ✅ Correct
const x: string[] = ['a', 'b'];
const y: readonly string[] = ['a', 'b'];
const z: Readonly<string[]> = ['a', 'b'];
Open in Playgroundconst x: Array<string> = ['a', 'b'];
const y: ReadonlyArray<string> = ['a', 'b'];
const z: Readonly<Array<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).
- ❌ 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 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组合矩阵
¥Combination Matrix
该矩数组出了所有可能的选项组合以及不同类型数组的预期结果。
¥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> |
何时不使用它
¥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.
'## 资源'