noMisleadingReturnType
Summary
Section titled “Summary”- Rule available since:
v2.4.11 - Diagnostic Category:
lint/nursery/noMisleadingReturnType - This rule doesn’t have a fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noMisleadingReturnType": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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.
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.
function getStatus() { return "loading"; }function run(): void { return; }Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.