Skip to content

noUselessTypeAssertion (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noUselessTypeAssertion": "error"
}
}
}
}

Disallow type assertions that don’t change the type of an expression.

A type assertion tells TypeScript to treat a value as a different type. This includes as assertions (value as string), angle-bracket assertions (<string>value), and non-null assertions (value!), which tell TypeScript that a value isn’t null or undefined.

When the value already has the asserted type, the assertion does nothing. It only adds noise, and it makes the assertions that do matter harder to notice.

This rule reports:

  • non-null assertions on values that can’t be null or undefined;
  • non-null assertions on the left side of an = assignment, because assigning a value never reads the old one;
  • as and angle-bracket assertions to a type that the value already has. This check only covers string, number, bigint, boolean, symbol, null, undefined, and unions of them, such as string | undefined. It only checks function calls, awaited function calls, +, comparisons, and other type assertions. It doesn’t check variables and properties, because TypeScript can give them a more specific type where the assertion is written, for example inside if (typeof value === "string").

The rule doesn’t report as const assertions.

invalid-non-null.ts
const foo = 3;
const bar = foo!;
/invalid-non-null.ts:2:13 lint/nursery/noUselessTypeAssertion  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This non-null assertion is unnecessary.

1 │ const foo = 3;
> 2 │ const bar = foo!;
│ ^^^^
3 │

ℹ This value can't be null or undefined, so the assertion has no effect.

ℹ Unnecessary assertions add noise and make the assertions that matter harder to notice.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the non-null assertion.

2 │ const·bar·=·foo!;
│ -
invalid-parameter.ts
function foo(x: number): number {
return x!;
}
/invalid-parameter.ts:2:12 lint/nursery/noUselessTypeAssertion  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This non-null assertion is unnecessary.

1 │ function foo(x: number): number {
> 2 │ return x!;
│ ^^
3 │ }
4 │

ℹ This value can't be null or undefined, so the assertion has no effect.

ℹ Unnecessary assertions add noise and make the assertions that matter harder to notice.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the non-null assertion.

2 │ ····return·x!;
│ -
invalid-angle-bracket.ts
const foo = <number>(3 + 5);
/invalid-angle-bracket.ts:1:13 lint/nursery/noUselessTypeAssertion  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━

⚠ This type assertion is unnecessary.

> 1 │ const foo = <number>(3 + 5);
│ ^^^^^^^^
2 │

ℹ The expression already has this type, so the assertion has no effect.

ℹ Unnecessary assertions add noise and make the assertions that matter harder to notice.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the type assertion.

1 │ const·foo·=·<number>(3·+·5);
│ --------
invalid-alias.ts
type Foo = number;
const foo = (3 + 5) as Foo;
/invalid-alias.ts:2:21 lint/nursery/noUselessTypeAssertion  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This type assertion is unnecessary.

1 │ type Foo = number;
> 2 │ const foo = (3 + 5) as Foo;
│ ^^^^^^
3 │

ℹ The expression already has this type, so the assertion has no effect.

ℹ Unnecessary assertions add noise and make the assertions that matter harder to notice.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the type assertion.

2 │ const·foo·=·(3·+·5)·as·Foo;
│ -------
invalid-double.ts
declare function getName(): string;
const foo = getName() as unknown as string;
/invalid-double.ts:2:13 lint/nursery/noUselessTypeAssertion  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ These type assertions are unnecessary.

1 │ declare function getName(): string;
> 2 │ const foo = getName() as unknown as string;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │

ℹ The original expression already has the final type, so the assertions have no effect.

ℹ Unnecessary assertions add noise and make the assertions that matter harder to notice.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the type assertions.

2 │ const·foo·=·getName()·as·unknown·as·string;
│ ---------------------
invalid-assignment.ts
let foo: number | undefined;
foo! = 1;
/invalid-assignment.ts:2:1 lint/nursery/noUselessTypeAssertion  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This non-null assertion is unnecessary.

1 │ let foo: number | undefined;
> 2 │ foo! = 1;
│ ^^^^
3 │

ℹ An assignment replaces the current value without reading it, so the value doesn't need to be non-null.

ℹ Unnecessary assertions add noise and make the assertions that matter harder to notice.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the non-null assertion.

2 │ foo!·=·1;
│ -
const a = <number>3;
const b = 3 as number;
let c = "foo" as const;
function foo(x: number | undefined): number {
return x!;
}