noUnsafeTypeAssertion
Summary
Section titled “Summary”- Rule available since:
v2.5.9 - Diagnostic Category:
lint/nursery/noUnsafeTypeAssertion - This rule doesn’t have a fix.
- The default severity of this rule is error.
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noUnsafeTypeAssertion": "error" } } }}Description
Section titled “Description”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:
- Type annotations
- The
satisfiesoperator - Type predicates
- Assertion functions
- Control-flow narrowing
- Validation libraries, like Zod, Valibot, or arktype
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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"); }}function narrow(value: string | undefined) { if (value !== undefined) { return value.length; }}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.