noUselessTypeAssertion (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noUselessTypeAssertion - This rule has an unsafe fix.
- The default severity of this rule is warning.
- This rule belongs to the following domains:
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noUselessTypeAssertion": "error" } } }}Description
Section titled “Description”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
nullorundefined; - non-null assertions on the left side of an
=assignment, because assigning a value never reads the old one; asand angle-bracket assertions to a type that the value already has. This check only coversstring,number,bigint,boolean,symbol,null,undefined, and unions of them, such asstring | 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 insideif (typeof value === "string").
The rule doesn’t report as const assertions.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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!;
│ -
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!;
│ -
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);
│ --------
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;
│ -------
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;
│ ---------------------
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!;}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.