> ## 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.

# TypeScript Return Statement

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

```typescript theme={"dark"}
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`.

```typescript theme={"dark"}
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.

```typescript theme={"dark"}
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.

```typescript theme={"dark"}
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.

```typescript theme={"dark"}
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`.

```typescript theme={"dark"}
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.

```typescript theme={"dark"}
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.

```typescript theme={"dark"}
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.

```typescript theme={"dark"}
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`.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
