Skip to content
← Back to rules

typescript/consistent-type-assertions Style

🛠️💡 An auto-fix and a suggestion are available for this rule for some violations.

What it does

Enforce consistent usage of TypeScript type assertions.

Why is this bad?

Mixing assertion styles (as vs angle-bracket) makes code harder to read and maintain. In some codebases, type assertions are banned in favor of safer alternatives like type annotations or satisfies.

Examples

Examples of incorrect code for this rule (default: assertionStyle: "as"):

ts
const value = <Foo>bar;

Examples of correct code for this rule (default: assertionStyle: "as"):

ts
const value = bar as Foo;

Examples of incorrect code for this rule with assertionStyle: "angle-bracket":

ts
const value = bar as Foo;

Examples of correct code for this rule with assertionStyle: "angle-bracket":

ts
const value = <Foo>bar;

Examples of incorrect code for this rule with assertionStyle: "never":

ts
const value = bar as Foo;

Examples of correct code for this rule with assertionStyle: "never":

ts
const value: Foo = bar;
const value = bar satisfies Foo;

When object/array literal assertions are disallowed, prefer annotations or satisfies:

ts
// incorrect (when `objectLiteralTypeAssertions: "never"`)
const obj = { a: 1 } as Foo;

// correct
const obj: Foo = { a: 1 };
const obj = { a: 1 } satisfies Foo;

Configuration

assertionStyle

type: "never"

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "typescript/consistent-type-assertions": "error"
  }
}
bash
oxlint --deny typescript/consistent-type-assertions

References