|
1 | 1 | # Requires using either `T[]` or `Array<T>` for arrays (array-type)
|
2 | 2 |
|
3 |
| -```ts |
4 |
| -class Foo<T = Array<Array<Bar>>> extends Bar<T, Array<T>> |
5 |
| - implements Baz<Array<T>> { |
6 |
| - private s: Array<T>; |
7 |
| - |
8 |
| - constructor(p: Array<T>) { |
9 |
| - return new Array(); |
10 |
| - } |
11 |
| -} |
12 |
| -``` |
| 3 | +Using the same style for array definitions across your codebase makes it easier for your developers to read and understand the types. |
13 | 4 |
|
14 | 5 | ## Rule Details
|
15 | 6 |
|
16 |
| -This rule aims to standardise usage of array. |
| 7 | +This rule aims to standardise usage of array types within your codebase. |
17 | 8 |
|
18 | 9 | ## Options
|
19 | 10 |
|
20 |
| -Default config: |
| 11 | +This rule accepts one option - a single string |
| 12 | + |
| 13 | +- `"array"` enforces use of `T[]` for all types `T`. |
| 14 | +- `"generic"` enforces use of `Array<T>` for all types `T`. |
| 15 | +- `"array-simple"` enforces use of `T[]` if `T` is a simple type. |
| 16 | + |
| 17 | +Without providing an option, by default the rule will enforce `"array"`. |
| 18 | + |
| 19 | +### `"array"` |
| 20 | + |
| 21 | +Always use `T[]` or `readonly T[]` for all array types. |
| 22 | + |
| 23 | +Incorrect code for `"array"`: |
| 24 | + |
| 25 | +```ts |
| 26 | +const x: Array<string> = ["a", "b"]; |
| 27 | +const y: ReadonlyArray<string> = ["a", "b"]; |
| 28 | +``` |
21 | 29 |
|
22 |
| -```JSON |
23 |
| -{ |
24 |
| - "array-type": ["error", "array"] |
25 |
| -} |
| 30 | +Correct code for `"array"`: |
| 31 | + |
| 32 | +```ts |
| 33 | +const x: string[] = ["a", "b"]; |
| 34 | +const y: readonly string[] = ["a", "b"]; |
26 | 35 | ```
|
27 | 36 |
|
28 |
| -- `array` enforces use of `T[]` for all types `T`. |
29 |
| -- `generic` enforces use of `Array<T>` for all types `T`. |
30 |
| -- `array-simple` enforces use of `T[]` if `T` is a simple type. |
| 37 | +### `"generic"` |
| 38 | + |
| 39 | +Always use `Array<T>` or `ReadonlyArray<T>` for all array types. |
| 40 | + |
| 41 | +Incorrect code for `"generic"`: |
| 42 | + |
| 43 | +```ts |
| 44 | +const x: string[] = ["a", "b"]; |
| 45 | +const y: readonly string[] = ["a", "b"]; |
| 46 | +``` |
| 47 | + |
| 48 | +Correct code for `"generic"`: |
| 49 | + |
| 50 | +```ts |
| 51 | +const x: Array<string> = ["a", "b"]; |
| 52 | +const y: ReadonlyArray<string> = ["a", "b"]; |
| 53 | +``` |
| 54 | + |
| 55 | +### `"array-simple"` |
| 56 | + |
| 57 | +Use `T[]` or `readonly T[]` for simple types (i.e. types which are just primitive names or type references). |
| 58 | +Use `Array<T>` or `ReadonlyArray<T>` for all other types (union types, intersection types, object types, function types, etc). |
| 59 | + |
| 60 | +Incorrect code for `"array-simple"`: |
| 61 | + |
| 62 | +```ts |
| 63 | +const a: (string | number)[] = ["a", "b"]; |
| 64 | +const b: ({ prop: string })[] = [{ prop: "a" }]; |
| 65 | +const c: (() => void)[] = [() => {}]; |
| 66 | +const d: Array<MyType> = ["a", "b"]; |
| 67 | +const e: Array<string> = ["a", "b"]; |
| 68 | +const f: ReadonlyArray<string> = ["a", "b"]; |
| 69 | +``` |
| 70 | + |
| 71 | +Correct code for `"array-simple"`: |
| 72 | + |
| 73 | +```ts |
| 74 | +const a: Array<string | number> = ["a", "b"]; |
| 75 | +const b: Array<{ prop: string }> = [{ prop: "a" }]; |
| 76 | +const c: Array<() => void> = [() => {}]; |
| 77 | +const d: MyType[] = ["a", "b"]; |
| 78 | +const e: string[] = ["a", "b"]; |
| 79 | +const f: readonly string[] = ["a", "b"]; |
| 80 | +``` |
31 | 81 |
|
32 | 82 | ## Related to
|
33 | 83 |
|
|
0 commit comments