Skip to content

Commit 44b099d

Browse files
authored
docs(eslint-plugin): Expand docs for array-type (typescript-eslint#634)
I noticed the docs were pretty sparse. Simple, but not hugely helpful. I just expanded them with examples
1 parent b83ff5a commit 44b099d

File tree

1 file changed

+69
-19
lines changed

1 file changed

+69
-19
lines changed

packages/eslint-plugin/docs/rules/array-type.md

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,83 @@
11
# Requires using either `T[]` or `Array<T>` for arrays (array-type)
22

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.
134

145
## Rule Details
156

16-
This rule aims to standardise usage of array.
7+
This rule aims to standardise usage of array types within your codebase.
178

189
## Options
1910

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+
```
2129

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"];
2635
```
2736

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+
```
3181

3282
## Related to
3383

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy