Skip to main content

useShorthandArrayType (since v0.7.0)

This rule is recommended by Rome.

When expressing array types, this rule promotes the usage of T[] shorthand instead of Array<T>.

Examples

Invalid

let valid: Array<foo>;
error[ts/useShorthandArrayType]: Use shorthand T[] syntax instead of Array<T> syntax.
   ts/useShorthandArrayType.js:1:12
  
1  let valid: Array<foo>;
              ^^^^^^^^^^

Suggested fix: Use shorthand T[] syntax to replace
    | @@ -1 +1 @@
0   | - let valid: Array<foo>;
  0 | + let valid: foo[];

let invalid2: Promise<Array<string>>;
error[ts/useShorthandArrayType]: Use shorthand T[] syntax instead of Array<T> syntax.
   ts/useShorthandArrayType.js:1:23
  
1  let invalid2: Promise<Array<string>>;
                         ^^^^^^^^^^^^^

Suggested fix: Use shorthand T[] syntax to replace
    | @@ -1 +1 @@
0   | - let invalid2: Promise<Array<string>>;
  0 | + let invalid2: Promise<string[]>;

let invalid3: Array<Foo<Bar>>;
error[ts/useShorthandArrayType]: Use shorthand T[] syntax instead of Array<T> syntax.
   ts/useShorthandArrayType.js:1:15
  
1  let invalid3: Array<Foo<Bar>>;
                 ^^^^^^^^^^^^^^^

Suggested fix: Use shorthand T[] syntax to replace
    | @@ -1 +1 @@
0   | - let invalid3: Array<Foo<Bar>>;
  0 | + let invalid3: Foo<Bar>[];

let invalid: Array<[number, number]>;
error[ts/useShorthandArrayType]: Use shorthand T[] syntax instead of Array<T> syntax.
   ts/useShorthandArrayType.js:1:14
  
1  let invalid: Array<[number, number]>;
                ^^^^^^^^^^^^^^^^^^^^^^^

Suggested fix: Use shorthand T[] syntax to replace
    | @@ -1 +1 @@
0   | - let invalid: Array<[number, number]>;
  0 | + let invalid: [number, number][];

let invalid: Array<[number, number]>;
error[ts/useShorthandArrayType]: Use shorthand T[] syntax instead of Array<T> syntax.
   ts/useShorthandArrayType.js:1:14
  
1  let invalid: Array<[number, number]>;
                ^^^^^^^^^^^^^^^^^^^^^^^

Suggested fix: Use shorthand T[] syntax to replace
    | @@ -1 +1 @@
0   | - let invalid: Array<[number, number]>;
  0 | + let invalid: [number, number][];

Valid

let valid: Array<Foo | Bar>;
let valid: Array<keyof Bar>;
let valid: Array<foo | bar>;