Skip to content

docs(eslint-plugin): [consistent-type-assertion] improve docs #1651

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 1 commit into from
Feb 28, 2020
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
12 changes: 12 additions & 0 deletions packages/eslint-plugin/docs/rules/consistent-type-assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This rule aims to standardize the use of type assertion style across the codebas

Type assertions are also commonly referred as "type casting" in TypeScript (even though it is technically slightly different to what is understood by type casting in other languages), so you can think of type assertions and type casting referring to the same thing. It is essentially you saying to the TypeScript compiler, "in this case, I know better than you!".

In addition to ensuring that type assertions are written in a consistent way, this rule also helps make your codebase more type-safe.

## Options

```ts
Expand Down Expand Up @@ -44,10 +46,16 @@ The compiler will warn for excess properties with this syntax, but not missing _

The const assertion `const x = { foo: 1 } as const`, introduced in TypeScript 3.4, is considered beneficial and is ignored by this option.

Assertions to `any` are also ignored by this option.

Examples of **incorrect** code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'never' }` (and for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'allow-as-parameter' }`)

```ts
const x = { ... } as T;

function foo() {
return { ... } as T;
}
```

Examples of **correct** code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'never' }`.
Expand All @@ -56,6 +64,10 @@ Examples of **correct** code for `{ assertionStyle: 'as', objectLiteralTypeAsser
const x: T = { ... };
const y = { ... } as any;
const z = { ... } as unknown;

function foo(): T {
return { ... };
}
```

Examples of **correct** code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'allow-as-parameter' }`.
Expand Down