ProgramGuru

Function Return Types

Understand Function Return Types

A return type annotation describes the value a function promises to produce. TypeScript checks whether its return expressions meet that promise.

What you will learn: the purpose of function return types, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how function return types works and where its type rules apply.
  • Predict the exact output of the function return types example.
  • Diagnose a common issue: If a function promises number but returns a string, do not silence the error with a type assertion.

Build the mental model

Types are erased when TypeScript emits JavaScript. Returning a number does not wrap it in a special typed container. Explicit return annotations can catch accidental changes to an API, while inference is often sufficient for small local functions.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Function Return Types: explained visuallyCall isAdult(21) → Check age >= 18 → Boolean: trueCall isAdult(21)Check age >= 18Boolean: trueFollow the values through the operation
Function Return Types: Call isAdult(21) → Check age >= 18 → Boolean: true.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

function isAdult(age: number): boolean {
  return age >= 18;
}
const result: boolean = isAdult(21);
console.log(result);
console.log(isAdult(15));

Verified output

true
false

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

isAdult(21) evaluates 21 >= 18 as true; isAdult(15) evaluates the comparison as false. The declared boolean return type matches both branches.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

If a function promises number but returns a string, do not silence the error with a type assertion. Fix the returned value or the intended contract.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Write a classifyScore(score) function returning "pass" or "retry". Set an explicit string return type, then try an incorrect numeric return.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

What happens to a function return type annotation at runtime?
Choose one answer. You can try again.

Answer and explanation

It is erased; the returned JavaScript value remains.

A return type annotation describes the value a function promises to produce. TypeScript checks whether its return expressions meet that promise.

Review the key idea

Function Return Types: A return type annotation describes the value a function promises to produce.

isAdult(21) evaluates 21 >= 18 as true; isAdult(15) evaluates the comparison as false. The declared boolean return type matches both branches.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Optional Parameters →