• Error
  • Reported by the type checker

TS1163

A 'yield' expression is only allowed in a generator body.

A `yield` expression appears outside a generator function, for example in a nested normal function. Make the function a generator, or move the `yield`.

What it means

yield only works directly inside a generator (function* or a * method). A normal function nested inside a generator doesn't count: its body isn't part of the generator.

Example

index.ts
export function* g() {
  function inner() {
    yield 1;
  }
}
ts-r-check -p tsconfig.json --pretty false ts-r output
index.ts(3,5): error TS1163: A 'yield' expression is only allowed in a generator body.

Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.

How to fix

  • Mark the enclosing function as a generator: function* inner(), and delegate with yield* inner().
  • Or move the yield into the generator's own body.