useConsistentArrayType
¥Summary
-
规则生效日期:
v1.5.0¥Rule available since:
v1.5.0 -
诊断类别:
lint/style/useConsistentArrayType¥Diagnostic Category:
lint/style/useConsistentArrayType -
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
@typescript-eslint/array-type相同¥Same as
@typescript-eslint/array-type
-
¥How to configure
{ "linter": { "rules": { "style": { "useConsistentArrayType": "error" } } }}¥Description
要求一致使用 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
let invalid: Array<foo>;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 │
let invalid: Promise<Array<string>>;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 │
let invalid3: Array<Foo<Bar>>;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
const valid: Array<string | number> = ['a', 'b'];const valid: Array<{ prop: string }> = [{ prop: 'a' }];const valid: Array<() => void> = [() => {}];const valid: MyType[] = ['a', 'b'];const valid: string[] = ['a', 'b'];const valid: readonly string[] = ['a', 'b'];¥Options
使用以下选项指定要使用的数组声明语法。
¥Use the options to specify the syntax of array declarations to use.
{ "linter": { "rules": { "style": { "useConsistentArrayType": { "options": { "syntax": "shorthand" } } } } }}syntax
Section titled “syntax”使用的语法:
¥The syntax to use:
-
generic:数组声明将转换为Array<T>或ReadonlyArray<T>¥
generic: array declarations will be converted toArray<T>orReadonlyArray<T> -
shorthand:数组声明将转换为T[]或readonly T[]¥
shorthand: array declarations will be converted toT[]orreadonly T[]
默认:shorthand
¥Default: shorthand
¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号