> ## 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 Generator Function

A generator function is a special class of function that can pause its execution context and yield multiple values over time, rather than computing and returning a single value synchronously. When invoked, it does not execute its body immediately; instead, it returns a Generator object that conforms to both the `Iterable` and `Iterator` protocols.

## Syntax and Declaration

Generators are declared using the `function*` syntax. Inside the function body, the `yield` keyword is used to emit a value and suspend the function's execution state.

```typescript theme={"dark"}
function* simpleGenerator() {
    yield 1;
    yield 2;
}
```

## TypeScript Typing

In TypeScript, the return type of a generator function is explicitly defined using the generic `Generator<T, TReturn, TNext>` interface.

* **`T` (Yield Type):** The type of values emitted by the `yield` statements.
* **`TReturn` (Return Type):** The type of the final value returned by the function when it completes (via the `return` statement). Defaults to `void` or `any` if omitted.
* **`TNext` (Next Type):** The type of the value accepted by the `next()` method, which is injected back into the generator at the point of the suspended `yield`. Defaults to `unknown`.

```typescript theme={"dark"}
function* typedGenerator(): Generator<string, number, boolean> {
    // Yields a string, expects a boolean to be passed into next()
    const injectedValue: boolean = yield "Step 1"; 
    
    // Yields another string
    yield `Step 2 received: ${injectedValue}`; 
    
    // Returns a number when done
    return 42; 
}
```

## Execution Mechanics

Interacting with a generator involves calling the `next()` method on the instantiated Generator object. Each call to `next()` returns an `IteratorResult` object containing two properties:

* `value`: The yielded or returned value.
* `done`: A boolean indicating whether the generator has completed its execution.

```typescript theme={"dark"}
const gen = typedGenerator();

// 1. Starts execution, pauses at the first yield.
// The argument to the first next() is always ignored.
const res1 = gen.next(); 
// res1: { value: "Step 1", done: false }

// 2. Resumes execution, injects `true` into `injectedValue`.
// Pauses at the second yield.
const res2 = gen.next(true); 
// res2: { value: "Step 2 received: true", done: false }

// 3. Resumes execution, hits the return statement.
const res3 = gen.next(false); 
// res3: { value: 42, done: true }
```

## Delegation with `yield*`

A generator can delegate its yielding behavior to another generator or iterable object using the `yield*` expression. TypeScript infers the yielded types of the delegated iterable and merges them into the parent generator's type signature.

```typescript theme={"dark"}
function* subGenerator(): Generator<number, void, unknown> {
    yield 2;
    yield 3;
}

function* mainGenerator(): Generator<number, void, unknown> {
    yield 1;
    yield* subGenerator(); // Delegates execution to subGenerator
    yield 4;
}
// mainGenerator yields: 1, 2, 3, 4
```

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