Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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

return expression;
  • 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.
function getCount(): number {
    return 42; // Valid: 42 is assignable to number
}

function getString(): string {
    return 42; // TS2322: Type 'number' is not assignable to type 'string'.
}

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.
async function fetchId(): Promise<number> {
    return 42; // Valid: 42 is assignable to T (number)
}

async function fetchIdAsync(): Promise<number> {
    return Promise.resolve(42); // Valid: assignable to Promise<T>
}

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.
function* processItems(): Generator<number, string, unknown> {
    yield 1;
    yield 2;
    return "Complete"; // Valid: "Complete" is assignable to TReturn (string)
}

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.
function evaluate(flag: boolean) {
    if (flag) {
        return "Success"; // Infers string
    }
    return 0; // Infers number
}
// The compiler infers the return type as: string | number

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.
function terminateProcess(): void {
    return; // Valid: Empty return statement
}

function clearData(): void {
    return undefined; // Valid: undefined is assignable to void
}

function logProcess(): void {
    return console.log("Done"); // Valid: console.log() 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.
type Callback = () => void;

const myCallback: Callback = () => {
    return true; // Valid: Contextually typed as void, the boolean return is permitted but ignored
};

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.
function throwError(): never {
    throw new Error("Fatal exception");
    // A return statement here is unreachable and violates the 'never' type
}

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.
function checkValue(val: number): boolean {
    if (val > 0) {
        return true;
    }
    // TS2366: Function lacks ending return statement and return type does not include 'undefined'.
}
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.
Master TypeScript with Deep Grasping Methodology!Learn More