Skip to content

noMisleadingReturnType

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

Detect return type annotations that are misleadingly wider than what the implementation actually returns.

Reports when a function’s explicit return type annotation is wider than what TypeScript would infer from the implementation, hiding precise types from callers.

invalid.ts
function getStatus(b: boolean): string { if (b) return "loading"; return "idle"; }
/invalid.ts:1:31 lint/nursery/noMisleadingReturnType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The return type annotation is wider than what the function actually returns.

> 1 │ function getStatus(b: boolean): string { if (b) return “loading”; return “idle”; }
^^^^^^^^
2 │

A wider return type hides the precise types that callers could rely on.

Consider using “loading” | “idle” as the return type.

This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/9810 for more information or to report possible bugs.

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.

invalid2.ts
function getCode(ok: boolean): number { if (ok) return 200; return 404; }
/invalid2.ts:1:30 lint/nursery/noMisleadingReturnType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The return type annotation is wider than what the function actually returns.

> 1 │ function getCode(ok: boolean): number { if (ok) return 200; return 404; }
^^^^^^^^
2 │

A wider return type hides the precise types that callers could rely on.

Consider using 200 | 404 as the return type.

This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/9810 for more information or to report possible bugs.

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.

invalid3.ts
class Foo { getStatus(b: boolean): string { if (b) return "loading"; return "idle"; } }
/invalid3.ts:1:34 lint/nursery/noMisleadingReturnType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The return type annotation is wider than what the function actually returns.

> 1 │ class Foo { getStatus(b: boolean): string { if (b) return “loading”; return “idle”; } }
^^^^^^^^
2 │

A wider return type hides the precise types that callers could rely on.

Consider using “loading” | “idle” as the return type.

This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/9810 for more information or to report possible bugs.

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.

invalid4.ts
const obj = { getMode(b: boolean): string { if (b) return "dark"; return "light"; } };
/invalid4.ts:1:34 lint/nursery/noMisleadingReturnType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The return type annotation is wider than what the function actually returns.

> 1 │ const obj = { getMode(b: boolean): string { if (b) return “dark”; return “light”; } };
^^^^^^^^
2 │

A wider return type hides the precise types that callers could rely on.

Consider using “dark” | “light” as the return type.

This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/9810 for more information or to report possible bugs.

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.

invalid5.ts
function makeData(): object { return { retry: true }; }
/invalid5.ts:1:20 lint/nursery/noMisleadingReturnType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The return type annotation is wider than what the function actually returns.

> 1 │ function makeData(): object { return { retry: true }; }
^^^^^^^^
2 │

A wider return type hides the precise types that callers could rely on.

Narrow the return type to match what the function actually returns.

This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/9810 for more information or to report possible bugs.

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 getStatus() { return "loading"; }
function run(): void { return; }
class Foo { greet(): string { return "hello"; } }
  • Suggested replacement types are only shown when their textual representation is up to 80 characters long. Longer unions fall back to a generic note without the specific suggestion.
  • When a return uses a type assertion such as as T, the rule does not flag the return unless it can prove that T is narrower than object. Trusted cases include unknown, any, typeof queries, conditional types, generic type parameters, and types the rule cannot resolve. Intersections (A & B) are trusted when every member is or when any member is any; unions (A | B) when at least one is.