For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/typescript-eslint/no-unnecessary-type-assertion.md.
close

no-unnecessary-type-assertion

Added in v0.1.4

Configuration

PresetConfigured Value
✅ ts.configs.recommendedTypeChecked"error"
✅ ts.configs.strictTypeChecked"error"
rslint.config.ts
import { defineConfig, ts } from '@rslint/core';

export default defineConfig([
  ts.configs.recommended,
  {
    rules: {
      '@typescript-eslint/no-unnecessary-type-assertion': 'error',
    },
  },
]);

Rule Details

This rule reports unnecessary assertions, including as, angle-bracket assertions, and non-null assertions (!).

Examples of incorrect code for this rule:

const foo = 3;
const bar = foo!; // foo is already non-nullable

declare const value: number;
const num = value as number; // already a number

Examples of correct code for this rule:

const foo: number | undefined = getValue();
const bar = foo!; // non-null assertion is meaningful

const value = someUnknown as string; // type narrowing

const x = 3 as const;

Options

OptionDefaultDescription
typesToIgnore[]Skip as and angle-bracket assertions for the listed type names.
checkLiteralConstAssertionsfalseAlso check for unnecessary as const assertions on literal values.

Differences from upstream

Rslint keeps assertions that your code relies on. This helps avoid new type errors, lost type information, or changes in behavior after running --fix.

Some assertions are reported without an automatic fix, so their diagnostics remain after --fix. This includes certain assertions split across lines with comments. The rule leaves these assertions in place to preserve your program's behavior.

Original Documentation