Skip to main content
The return statement is a control flow construct that terminates the execution of a function and passes an evaluated expression back to the caller. In TypeScript, the return statement is bound to the function’s type signature, enforcing strict static type checking between the evaluated expression and the expected return type.

Syntax

  • expression (Optional): The value to be returned. If omitted, the function implicitly returns undefined at runtime.

Static Type Enforcement

TypeScript evaluates return statements using either explicit type annotations or type inference. The rules for assignability depend on the function’s execution model.

Synchronous Functions

When a synchronous function signature includes an explicit return type T, the compiler verifies that the expression in every return statement is assignable to T.

Asynchronous Functions

In an async function with a declared return type of Promise<T>, the evaluated return expression must be assignable to T or Promise<T>. The compiler does not require the expression to be a Promise itself, as the runtime automatically wraps the returned value.

Generator Functions

In a generator function (function*) with a declared return type of Generator<TYield, TReturn, TNext>, the expression in the return statement must be assignable to the TReturn type parameter.

Type Inference

If no explicit return type is provided, TypeScript infers the function’s return type by analyzing the types of the expressions in all return statements within the function body. If multiple return statements exist, the inferred type becomes a union of all returned types.

Special Return Types and Contextual Typing

TypeScript utilizes specific types and contextual rules to handle the absence, impossibility, or ignoring of a return statement.

The void Type

The void type indicates that a function does not return a usable value. A function explicitly returning void can omit the return statement, use an empty return;, explicitly return undefined;, or return an expression that evaluates to void.

Contextual Typing with void

When a function expression is contextually typed with a void return type, TypeScript’s assignability rules permit its return statements to evaluate to any type. The compiler allows the return of a value without raising an error, but enforces that the caller cannot use the returned value.

The never Type

The never type indicates that a function will never successfully execute a return statement to completion. This applies to functions that unconditionally throw exceptions or contain infinite loops.

Control Flow Analysis

TypeScript performs strict control flow analysis to ensure that all code paths in a function with a declared, non-void return type culminate in a valid return statement.
If the strictNullChecks compiler option is enabled, failing to return a value on all branches results in a compilation error unless the return type explicitly includes undefined.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More