Codectionary / Developer documentation / TypeScript

Checking Return Paths with noImplicitReturns

noImplicitReturns checks functions for inconsistent return paths.

What it is and why to use it

noImplicitReturns checks functions for inconsistent return paths. It is useful when refactoring branches could accidentally leave some inputs returning undefined. Combined with explicit return types, it encourages a function to explain the result of every intended path.

Common mistakes and limitations

This is not a requirement that every procedure return a value; a consistently void function is valid. A function that always throws also has no normal return. Choose an explicit return type when missing values would violate the contract.

Run this example

Use TypeScript 5.8 or later with strict checking and an ES2022 target. Save each complete example independently as example.ts, compile it, then run the generated JavaScript with a compatible Node.js installation. Compiler-only options below affect checking; they do not add runtime validation.

Terminal — compile and run

npx tsc example.ts --strict --target ES2022 --module commonjs
node example.js

Syntax

"noImplicitReturns": true

Examples

Checking Return Paths with noImplicitReturns

Standalone TypeScript 5.8+ example; expected output is explained below.

function status(score: number): string {
  if (score >= 50) return "Pass";
  return "Try again";
}
console.log(status(72));
console.log(status(40));
  1. The first branch returns Pass when the threshold is met.
  2. The final return covers every remaining number.
  3. Compile with --noImplicitReturns to check this policy explicitly. The option is separate from strict and does not insert a return statement into emitted JavaScript.
  4. Expected output: Pass Try again

Best practices

  • This is not a requirement that every procedure return a value; a consistently void function is valid. A function that always throws also has no normal return. Choose an explicit return type when missing values would violate the contract.
  • Keep external-input validation separate from compile-time type descriptions.

Runtime & versions

Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

TypeScript Handbook

Specifications & further reading