Skip to content

Commit

Permalink
docs(eslint-plugin): [consistent-type-assertion] improve docs (#1651)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrowne committed Feb 28, 2020
1 parent ae7f7c5 commit baf7c98
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/eslint-plugin/docs/rules/consistent-type-assertions.md
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

0 comments on commit baf7c98

Please sign in to comment.