useConsistentArrayType
このコンテンツはまだ日本語訳がありません。
Diagnostic Category: lint/style/useConsistentArrayType
Since: v1.5.0
Sources:
- Same as:
@typescript-eslint/array-type
Description
Section titled “Description”Require consistently using either T[]
or 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
Section titled “Example”Invalid
Section titled “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 │
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
Section titled “Options”Use the options to specify the syntax of array declarations to use.
{ "//": "...", "options": { "syntax": "shorthand" }}
syntax
Section titled “syntax”The syntax to use:
generic
: array declarations will be converted toArray<T>
orReadonlyArray<T>
shorthand
: array declarations will be converted toT[]
orreadonly T[]
Default: shorthand
How to configure
Section titled “How to configure”{ "linter": { "rules": { "style": { "useConsistentArrayType": "error" } } }}