require-no-implicit-returns
Require "noImplicitReturns": true in compilerOptions so that TypeScript
reports an error when a function does not explicitly return a value on all
code paths.
Targeted pattern scopeโ
The compilerOptions.noImplicitReturns field in any tsconfig*.json file.
What this rule reportsโ
This rule reports when compilerOptions.noImplicitReturns is absent or
explicitly set to false.
Why this rule existsโ
When a function is declared with a non-void return type but some of its
code paths reach the end without returning a value, JavaScript silently
returns undefined from that branch:
function getLabel(code: number): string {
if (code === 1) {
return "success";
}
// โ no return here โ TypeScript would allow this without noImplicitReturns
// at runtime, callers receive `undefined` instead of a string
}
With noImplicitReturns: true, TypeScript reports an error on this function,
requiring every code path to explicitly return a value (or be typed as
returning string | undefined).
Common scenarios where this catches real bugs:
- Functions with complex
if/elseorswitchchains where a new branch is added but the return statement is forgotten. - Methods in classes where the default return is accidentally
undefineddue to an incomplete migration. - Arrow functions used as callbacks that are expected to always produce a value.
The auto-fixer adds "noImplicitReturns": true to compilerOptions.
โ Incorrectโ
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext"
}
}
noImplicitReturns is absent; functions that silently return undefined
on some paths are not reported.
{
"compilerOptions": {
"noImplicitReturns": false
}
}
Explicitly disabled.
โ Correctโ
{
"compilerOptions": {
"noImplicitReturns": true
}
}
All code paths in non-void functions must explicitly return a value.
When not to use itโ
Disable this rule in projects that deliberately use implicit undefined
returns as a shorthand for early-exit patterns, or when migrating a large
JavaScript codebase where fixing every implicit return at once is not
feasible. In those cases, track the suppressions and enable the rule
incrementally.
Package documentationโ
Rule catalog ID: R033