useConsistentArrayType
诊断类别:lint/style/useConsistentArrayType
¥Diagnostic Category: lint/style/useConsistentArrayType
自从:v1.5.0
¥Since: v1.5.0
来源:
¥Sources:
-
与以下相同:
@typescript-eslint/array-type
¥Same as:
@typescript-eslint/array-type
要求一致使用 T[]
或 Array<T>
¥Require consistently using either T[]
or Array<T>
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.
¥Example
¥Invalid
code-block.ts:1:14 lint/style/useConsistentArrayType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use shorthand T[] syntax instead of Array<T> syntax.
> 1 │ let invalid: Array<foo>;
│ ^^^^^^^^^^
2 │
ℹ Unsafe fix: Use shorthand T[] syntax to replace
1 │ - let·invalid:·Array<foo>;
1 │ + let·invalid:·foo[];
2 2 │
code-block.ts:1:22 lint/style/useConsistentArrayType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use shorthand T[] syntax instead of Array<T> syntax.
> 1 │ let invalid: Promise<Array<string>>;
│ ^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Use shorthand T[] syntax to replace
1 │ - let·invalid:·Promise<Array<string>>;
1 │ + let·invalid:·Promise<string[]>;
2 2 │
code-block.ts:1:15 lint/style/useConsistentArrayType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use shorthand T[] syntax instead of Array<T> syntax.
> 1 │ let invalid3: Array<Foo<Bar>>;
│ ^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Use shorthand T[] syntax to replace
1 │ - let·invalid3:·Array<Foo<Bar>>;
1 │ + let·invalid3:·Foo<Bar>[];
2 2 │
¥Valid
¥Options
规则提供了两个选项,有助于指定要使用的数组声明类型。
¥The rule provides two options that help to specify what type of array declarations to use.
默认:“shorthand”
¥Default: “shorthand”
¥Syntax
默认情况下,所有数组声明都将转换为 T[]
或 readonly T[]
,这意味着 shorthand
,或者如果选项设置为 “generic”,则所有都将转换为 Array<T>
或 ReadonlyArray<T>
。
¥By default, all array declarations will be converted to T[]
or readonly T[]
, which it means shorthand
,
or if the options is set to the “generic”, that all will converted to Array<T>
or ReadonlyArray<T>
.
¥Related links