Skip to content

Latest commit

 

History

History
57 lines (38 loc) · 1.16 KB

no-unnecessary-type-assertion.md

File metadata and controls

57 lines (38 loc) · 1.16 KB

Warns if a type assertion does not change the type of an expression (no-unnecessary-type-assertion)

This rule prohibits using a type assertion that does not change the type of an expression.

Rule Details

This rule aims to prevent unnecessary type assertions.

Examples of incorrect code for this rule:

const foo = 3;
const bar = foo!;
const foo = <3>3;
type Foo = 3;
const foo = <Foo>3;
type Foo = 3;
const foo = 3 as Foo;

Examples of correct code for this rule:

const foo = <number>3;
const foo = 3 as number;

Options

This rule optionally takes an object with a single property typesToIgnore, which can be set to a list of type names to ignore.

For example, with @typescript-eslint/no-unnecessary-type-assertion: ["error", { typesToIgnore: ['Foo'] }], the following is correct code":

type Foo = 3;
const foo: Foo = 3;

When Not To Use It

If you don't care about having no-op type assertions in your code, then you can turn off this rule.

Related to