Skip to content

noUnsafeReturn (JavaScript)

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

Disallow returning a value typed as any from a function.

TypeScript doesn’t check how values typed as any are used. When a function returns such a value, every caller receives it without type checking, which can hide bugs far away from where the any came from.

This rule reports functions that return a value typed as any or any[], and async functions that return a Promise<any>. It also reports returned values that use any in a type argument where the return type expects a specific type, such as returning a Set<any> from a function whose return type is Set<string>.

Returning any is allowed when the return type is declared as any or unknown, and returning any[] is allowed when the return type is unknown[].

invalid-any.ts
function parse(text: string) {
return JSON.parse(text);
}
/invalid-any.ts:2:5 lint/nursery/noUnsafeReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This function returns a value of type any.

1 │ function parse(text: string) {
> 2 │ return JSON.parse(text);
│ ^^^^^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │

ℹ TypeScript doesn't check values of type any, so callers can misuse the returned value without any error.

ℹ Give the returned value a specific type. If the type isn't known in advance, use unknown instead, which requires callers to check the value before using it.

ℹ 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.

invalid-any-array.ts
const getItems = () => [] as any[];
/invalid-any-array.ts:1:24 lint/nursery/noUnsafeReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This function returns a value of type any[].

> 1 │ const getItems = () => [] as any[];
│ ^^^^^^^^^^^
2 │

ℹ TypeScript doesn't check values of type any, so callers can misuse the returned value without any error.

ℹ Give the returned value a specific type. If the type isn't known in advance, use unknown instead, which requires callers to check the value before using it.

ℹ 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.

invalid-promise-any.ts
declare const pending: Promise<any>;
async function load() {
return pending;
}
/invalid-promise-any.ts:3:5 lint/nursery/noUnsafeReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This function returns a value of type Promise<any>.

1 │ declare const pending: Promise<any>;
2 │ async function load() {
> 3 │ return pending;
│ ^^^^^^^^^^^^^^^
4 │ }
5 │

ℹ TypeScript doesn't check values of type any, so callers can misuse the returned value without any error.

ℹ Give the returned value a specific type. If the type isn't known in advance, use unknown instead, which requires callers to check the value before using it.

ℹ 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.

invalid-type-argument.ts
function getNames(): Set<string> {
return new Set<any>([1]);
}
/invalid-type-argument.ts:2:5 lint/nursery/noUnsafeReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This function returns a value that uses any where its return type expects a specific type.

1 │ function getNames(): Set<string> {
> 2 │ return new Set<any>([1]);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │

ℹ TypeScript doesn't check values of type any, so callers can misuse the returned value without any error.

ℹ Replace any in the type arguments of the returned value with the types that the return type expects.

ℹ 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.

valid.ts
function parse(text: string): unknown {
return JSON.parse(text);
}
function getNames(): Set<string> {
return new Set<string>(["a"]);
}
const getItems = () => ["a"];
  • If you want to disallow writing the any type, see noExplicitAny.