UNPKG

5 kBMarkdownView Raw
1# `array-type`
2
3Requires using either `T[]` or `Array<T>` for arrays.
4
5Using 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
9This rule aims to standardize usage of array types within your codebase.
10
11## Options
12
13```ts
14type ArrayOption = 'array' | 'generic' | 'array-simple';
15type Options = {
16 default: ArrayOption;
17 readonly?: ArrayOption;
18};
19
20const defaultOptions: Options = {
21 default: 'array',
22};
23```
24
25The 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
30Each property can be set to one of three strings: `'array' | 'generic' | 'array-simple'`.
31
32The default config will enforce that all mutable and readonly arrays use the `'array'` syntax.
33
34### `"array"`
35
36Always use `T[]` or `readonly T[]` for all array types.
37
38<!--tabs-->
39
40#### ❌ Incorrect
41
42```ts
43const x: Array<string> = ['a', 'b'];
44const y: ReadonlyArray<string> = ['a', 'b'];
45```
46
47#### ✅ Correct
48
49```ts
50const x: string[] = ['a', 'b'];
51const y: readonly string[] = ['a', 'b'];
52```
53
54### `"generic"`
55
56Always use `Array<T>` or `ReadonlyArray<T>` for all array types.
57
58<!--tabs-->
59
60#### ❌ Incorrect
61
62```ts
63const x: string[] = ['a', 'b'];
64const y: readonly string[] = ['a', 'b'];
65```
66
67#### ✅ Correct
68
69```ts
70const x: Array<string> = ['a', 'b'];
71const y: ReadonlyArray<string> = ['a', 'b'];
72```
73
74### `"array-simple"`
75
76Use `T[]` or `readonly T[]` for simple types (i.e. types which are just primitive names or type references).
77Use `Array<T>` or `ReadonlyArray<T>` for all other types (union types, intersection types, object types, function types, etc).
78
79<!--tabs-->
80
81#### ❌ Incorrect
82
83```ts
84const a: (string | number)[] = ['a', 'b'];
85const b: { prop: string }[] = [{ prop: 'a' }];
86const c: (() => void)[] = [() => {}];
87const d: Array<MyType> = ['a', 'b'];
88const e: Array<string> = ['a', 'b'];
89const f: ReadonlyArray<string> = ['a', 'b'];
90```
91
92#### ✅ Correct
93
94```ts
95const a: Array<string | number> = ['a', 'b'];
96const b: Array<{ prop: string }> = [{ prop: 'a' }];
97const c: Array<() => void> = [() => {}];
98const d: MyType[] = ['a', 'b'];
99const e: string[] = ['a', 'b'];
100const f: readonly string[] = ['a', 'b'];
101```
102
103## Combination Matrix
104
105This 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