Skip to content

tail-call ​

A function marked /** @loop */ that can't be turned into a loop.

Why: @loop promises constant stack for deep recursion. If a self-call isn't in tail position the promise can't be kept, and quietly leaving the recursion would let it overflow the stack later. Guide: Recursion as a loop.

❌ Rejected: the multiplication happens after the recursive call returns

✕ Rejectedtail-call
ts
/** @loop */
export function factorial(n: number): number {
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}

✅ Accepted: the running product is passed along

✓ Accepted0 diagnostics
ts
/** @loop */
export function factorial(n: number, product: number): number {
  if (n <= 1) return product;
  return factorial(n - 1, product * n);
}
ts
/** @loop */
export function factorial(n: number, product: number): number { __tc1: for (;;) {
  if (n <= 1) return product;
  { const __tc1_0 = n - 1, __tc1_1 = product * n; n = __tc1_0; product = __tc1_1; continue __tc1; }
 break; }}

The message names what stands in the way: a self-call not in tail position or inside try, a nested function, this or arguments, a spread argument, a call with the wrong number of arguments, or @loop on something that isn't a named function.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra