1 | # `array-type`
|
2 |
|
3 | Requires using either `T[]` or `Array<T>` for arrays.
|
4 |
|
5 | Using the same style for array definitions across your codebase makes it easier for your developers to read and understand the types.
|
6 |
|
7 | ## Rule Details
|
8 |
|
9 | This rule aims to standardize usage of array types within your codebase.
|
10 |
|
11 | ## Options
|
12 |
|
13 | ```ts
|
14 | type ArrayOption = 'array' | 'generic' | 'array-simple';
|
15 | type Options = {
|
16 | default: ArrayOption;
|
17 | readonly?: ArrayOption;
|
18 | };
|
19 |
|
20 | const defaultOptions: Options = {
|
21 | default: 'array',
|
22 | };
|
23 | ```
|
24 |
|
25 | The rule accepts an options object with the following properties:
|
26 |
|
27 | - `default` - sets the array type expected for mutable cases.
|
28 | - `readonly` - sets the array type expected for readonly arrays. If this is omitted, then the value for `default` will be used.
|
29 |
|
30 | Each property can be set to one of three strings: `'array' | 'generic' | 'array-simple'`.
|
31 |
|
32 | The default config will enforce that all mutable and readonly arrays use the `'array'` syntax.
|
33 |
|
34 | ### `"array"`
|
35 |
|
36 | Always use `T[]` or `readonly T[]` for all array types.
|
37 |
|
38 |
|
39 |
|
40 | #### ❌ Incorrect
|
41 |
|
42 | ```ts
|
43 | const x: Array<string> = ['a', 'b'];
|
44 | const y: ReadonlyArray<string> = ['a', 'b'];
|
45 | ```
|
46 |
|
47 | #### ✅ Correct
|
48 |
|
49 | ```ts
|
50 | const x: string[] = ['a', 'b'];
|
51 | const y: readonly string[] = ['a', 'b'];
|
52 | ```
|
53 |
|
54 | ### `"generic"`
|
55 |
|
56 | Always use `Array<T>` or `ReadonlyArray<T>` for all array types.
|
57 |
|
58 |
|
59 |
|
60 | #### ❌ Incorrect
|
61 |
|
62 | ```ts
|
63 | const x: string[] = ['a', 'b'];
|
64 | const y: readonly string[] = ['a', 'b'];
|
65 | ```
|
66 |
|
67 | #### ✅ Correct
|
68 |
|
69 | ```ts
|
70 | const x: Array<string> = ['a', 'b'];
|
71 | const y: ReadonlyArray<string> = ['a', 'b'];
|
72 | ```
|
73 |
|
74 | ### `"array-simple"`
|
75 |
|
76 | Use `T[]` or `readonly T[]` for simple types (i.e. types which are just primitive names or type references).
|
77 | Use `Array<T>` or `ReadonlyArray<T>` for all other types (union types, intersection types, object types, function types, etc).
|
78 |
|
79 |
|
80 |
|
81 | #### ❌ Incorrect
|
82 |
|
83 | ```ts
|
84 | const a: (string | number)[] = ['a', 'b'];
|
85 | const b: { prop: string }[] = [{ prop: 'a' }];
|
86 | const c: (() => void)[] = [() => {}];
|
87 | const d: Array<MyType> = ['a', 'b'];
|
88 | const e: Array<string> = ['a', 'b'];
|
89 | const f: ReadonlyArray<string> = ['a', 'b'];
|
90 | ```
|
91 |
|
92 | #### ✅ Correct
|
93 |
|
94 | ```ts
|
95 | const a: Array<string | number> = ['a', 'b'];
|
96 | const b: Array<{ prop: string }> = [{ prop: 'a' }];
|
97 | const c: Array<() => void> = [() => {}];
|
98 | const d: MyType[] = ['a', 'b'];
|
99 | const e: string[] = ['a', 'b'];
|
100 | const f: readonly string[] = ['a', 'b'];
|
101 | ```
|
102 |
|
103 | ## Combination Matrix
|
104 |
|
105 | This matrix lists all possible option combinations and their expected results for different types of Arrays.
|
106 |
|
107 | | defaultOption | readonlyOption | Array with simple type | Array with non simple type | Readonly array with simple type | Readonly array with non simple type |
|
108 | | -------------- | -------------- | ---------------------- | -------------------------- | ------------------------------- | ----------------------------------- |
|
109 | | `array` | | `number[]` | `(Foo & Bar)[]` | `readonly number[]` | `readonly (Foo & Bar)[]` |
|
110 | | `array` | `array` | `number[]` | `(Foo & Bar)[]` | `readonly number[]` | `readonly (Foo & Bar)[]` |
|
111 | | `array` | `array-simple` | `number[]` | `(Foo & Bar)[]` | `readonly number[]` | `ReadonlyArray<Foo & Bar>` |
|
112 | | `array` | `generic` | `number[]` | `(Foo & Bar)[]` | `ReadonlyArray<number>` | `ReadonlyArray<Foo & Bar>` |
|
113 | | `array-simple` | | `number[]` | `Array<Foo & Bar>` | `readonly number[]` | `ReadonlyArray<Foo & Bar>` |
|
114 | | `array-simple` | `array` | `number[]` | `Array<Foo & Bar>` | `readonly number[]` | `readonly (Foo & Bar)[]` |
|
115 | | `array-simple` | `array-simple` | `number[]` | `Array<Foo & Bar>` | `readonly number[]` | `ReadonlyArray<Foo & Bar>` |
|
116 | | `array-simple` | `generic` | `number[]` | `Array<Foo & Bar>` | `ReadonlyArray<number>` | `ReadonlyArray<Foo & Bar>` |
|
117 | | `generic` | | `Array<number>` | `Array<Foo & Bar>` | `ReadonlyArray<number>` | `ReadonlyArray<Foo & Bar>` |
|
118 | | `generic` | `array` | `Array<number>` | `Array<Foo & Bar>` | `readonly number[]` | `readonly (Foo & Bar)[]` |
|
119 | | `generic` | `array-simple` | `Array<number>` | `Array<Foo & Bar>` | `readonly number[]` | `ReadonlyArray<Foo & Bar>` |
|
120 | | `generic` | `generic` | `Array<number>` | `Array<Foo & Bar>` | `ReadonlyArray<number>` | `ReadonlyArray<Foo & Bar>` |
|
121 |
|
122 | ## Related To
|
123 |
|
124 | - TSLint: [array-type](https://palantir.github.io/tslint/rules/array-type/)
|
125 |
|
126 | ## Attributes
|
127 |
|
128 | - Configs:
|
129 | - [ ] ✅ Recommended
|
130 | - [x] 🔒 Strict
|
131 | - [x] 🔧 Fixable
|
132 | - [ ] 💭 Requires type information
|