Skip to content

docs(eslint-plugin): Expand docs for array-type #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 69 additions & 19 deletions packages/eslint-plugin/docs/rules/array-type.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,83 @@
# Requires using either `T[]` or `Array<T>` for arrays (array-type)

```ts
class Foo<T = Array<Array<Bar>>> extends Bar<T, Array<T>>
implements Baz<Array<T>> {
private s: Array<T>;

constructor(p: Array<T>) {
return new Array();
}
}
```
Using the same style for array definitions across your codebase makes it easier for your developers to read and understand the types.

## Rule Details

This rule aims to standardise usage of array.
This rule aims to standardise usage of array types within your codebase.

## Options

Default config:
This rule accepts one option - a single string

- `"array"` enforces use of `T[]` for all types `T`.
- `"generic"` enforces use of `Array<T>` for all types `T`.
- `"array-simple"` enforces use of `T[]` if `T` is a simple type.

Without providing an option, by default the rule will enforce `"array"`.

### `"array"`

Always use `T[]` or `readonly T[]` for all array types.

Incorrect code for `"array"`:

```ts
const x: Array<string> = ["a", "b"];
const y: ReadonlyArray<string> = ["a", "b"];
```

```JSON
{
"array-type": ["error", "array"]
}
Correct code for `"array"`:

```ts
const x: string[] = ["a", "b"];
const y: readonly string[] = ["a", "b"];
```

- `array` enforces use of `T[]` for all types `T`.
- `generic` enforces use of `Array<T>` for all types `T`.
- `array-simple` enforces use of `T[]` if `T` is a simple type.
### `"generic"`

Always use `Array<T>` or `ReadonlyArray<T>` for all array types.

Incorrect code for `"generic"`:

```ts
const x: string[] = ["a", "b"];
const y: readonly string[] = ["a", "b"];
```

Correct code for `"generic"`:

```ts
const x: Array<string> = ["a", "b"];
const y: ReadonlyArray<string> = ["a", "b"];
```

### `"array-simple"`

Use `T[]` or `readonly T[]` for simple types (i.e. types which are just primitive names or type references).
Use `Array<T>` or `ReadonlyArray<T>` for all other types (union types, intersection types, object types, function types, etc).

Incorrect code for `"array-simple"`:

```ts
const a: (string | number)[] = ["a", "b"];
const b: ({ prop: string })[] = [{ prop: "a" }];
const c: (() => void)[] = [() => {}];
const d: Array<MyType> = ["a", "b"];
const e: Array<string> = ["a", "b"];
const f: ReadonlyArray<string> = ["a", "b"];
```

Correct code for `"array-simple"`:

```ts
const a: Array<string | number> = ["a", "b"];
const b: Array<{ prop: string }> = [{ prop: "a" }];
const c: Array<() => void> = [() => {}];
const d: MyType[] = ["a", "b"];
const e: string[] = ["a", "b"];
const f: readonly string[] = ["a", "b"];
```

## Related to

Expand Down