noUnsafeReturn (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.3 - Diagnostic Category:
lint/nursery/noUnsafeReturn - This rule doesn’t have a fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noUnsafeReturn": "error" } } }}Description
Section titled “Description”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[].
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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.
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.
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.
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.
function parse(text: string): unknown { return JSON.parse(text);}
function getNames(): Set<string> { return new Set<string>(["a"]);}
const getItems = () => ["a"];See Also
Section titled “See Also”- If you want to disallow writing the
anytype, seenoExplicitAny.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.