Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Type Checking Options

TypeScript noImplicitReturns option

Reference for noImplicitReturns: error TS7030 for functions where only some code paths return a value, with fixes, exhaustive switches and void functions.

If a function returns a value on one path and simply runs off the end on another, callers get undefined on the second path. JavaScript allows it; it is usually a bug. noImplicitReturns reports an error when some code paths in a function return a value and others reach the end without one.

The check is about consistency of explicit return statements. It is not part of strict, but it is cheap to turn on and catches forgotten else branches and missing default cases.

TypeScript
function sign(n: number): string {
  if (n > 0) return "plus";
  if (n < 0) return "minus";
  // end reached: no value on this path
}

Try it

TypeScript A path that forgets to return

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Exhaustive code needs no extra return

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

JSON
{ "compilerOptions": { "noImplicitReturns": true } }
Part Description
Value true or false. Default: false. Not included in strict.
Error TS7030: Not all code paths return a value. reported on the offending return; or the closing brace.
Exempt Functions whose return type is void or any, functions that never return a value, and endpoints the compiler proves unreachable (such as after an exhaustive switch).

Usage notes

  • A bare return; in a function that elsewhere returns a value is also reported, because it returns nothing on that path.
  • Return undefined explicitly when that is what you mean; the option accepts it.
  • Exhaustive switches over unions are understood: if all cases return and the switch covers the whole type, no extra return is needed.
  • Without the option, a declared return type of string still errors when the end is reachable (TS2366); noImplicitReturns matters mainly for return types that include undefined or void, and for inferred return types.
  • Promises: for async functions the rule applies to the async function body, not to the promise.
  • Arrow functions with expression bodies always return, so they are never reported.
  • Related lint rule: ESLint has consistent-return; the compiler option is cheaper and type-aware.