Skip to content

noUnsafeTypeAssertion

biome.json
{
"linter": {
"rules": {
"nursery": {
"noUnsafeTypeAssertion": "error"
}
}
}
}

Disallow TypeScript type assertions other than const assertions.

Type assertions override TypeScript’s inferred type without performing any runtime checks. This can hide invalid assumptions about a value and lead to runtime errors.

Safer alternatives include:

interface SomeType {
value: string;
}
declare const value;
const asserted = value as SomeType;
code-block.ts:5:24 lint/nursery/noUnsafeTypeAssertion ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid unsafe type assertions.

3 │ }
4 │ declare const value;
> 5 │ const asserted = value as SomeType;
^^^^^^^^^^^
6 │

Type assertions override the type for this expression, which can hide type errors and lead to runtime errors.

Use a type annotation, the satisfies operator, a type predicate, or control-flow narrowing instead.

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.

interface SomeType {
value: string;
}
declare const value;
const asserted = <SomeType>value;
code-block.ts:5:18 lint/nursery/noUnsafeTypeAssertion ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid unsafe type assertions.

3 │ }
4 │ declare const value;
> 5 │ const asserted = <SomeType>value;
^^^^^^^^^^
6 │

Type assertions override the type for this expression, which can hide type errors and lead to runtime errors.

Use a type annotation, the satisfies operator, a type predicate, or control-flow narrowing instead.

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.

interface SomeType {
value: string;
}
declare const asserted;
(asserted as SomeType).value = "foo";
code-block.ts:5:11 lint/nursery/noUnsafeTypeAssertion ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid unsafe type assertions.

3 │ }
4 │ declare const asserted;
> 5 │ (asserted as SomeType).value = “foo”;
^^^^^^^^^^^
6 │

Type assertions override the type for this expression, which can hide type errors and lead to runtime errors.

Use a type annotation, the satisfies operator, a type predicate, or control-flow narrowing instead.

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.

const assertions are allowed:

const tuple = ["value", 1] as const;

Use a type annotation:

const annotated: string = "value";

Use the satisfies operator:

const checked = { value: "value" } satisfies { value: string };

Use a type predicate:

function isString(value: unknown): value is string {
return typeof value === "string";
}

Use an assertion function:

function assertIsString(value: unknown): asserts value is string {
if (!isString(value)) {
throw new TypeError("Expected a string");
}
}

Use control-flow narrowing:

function narrow(value: string | undefined) {
if (value !== undefined) {
return value.length;
}
}