---
title: "TS8031 RECURSION: an error code of the TypeShade compiler"
description: "TS8031 RECURSION, an error code of the TypeShade compiler. A call cycle: a function that reaches itself, directly or through other functions."
url: https://typeshade.dev/reference/errors/ts8031/
language: en
---

# `TS8031`

Front-end code `RECURSION`

A call cycle: a function that reaches itself, directly or through other functions. WGSL has no call stack, so the compiler refuses the cycle before a target sees it.

The registry has no documentation comment for this constant, so the line above is written by this site from the messages the compiler raises it with.

The registry text and every compiler message on these pages are the compiler's own English.

## Example

example.shade.ts

```ts
"use typeshade"

function fold(x: f32): f32 {
  if (x < 1.) {
    return x
  }
  return fold(x * 0.5)
}

@fragment
export function main(uv: location<0, vec2>): vec4 {
  return vec4(uv, fold(uv.x * 8.), 1.)
}
```

Compiled at build time against the pinned compiler, this program gets:

**TS8031** error, line 7: Recursive call: "fold" -> "fold". WGSL has no call stack, so a function must not take part in a call cycle.

## How to fix it

The same program, corrected. It compiles with no diagnostic.

example.shade.ts

```ts
"use typeshade"

function fold(x: f32): f32 {
  let y = x
  for (let i = 0; i < 8; i++) {
    if (y < 1.) {
      break
    }
    y = y * 0.5
  }
  return y
}

@fragment
export function main(uv: location<0, vec2>): vec4 {
  return vec4(uv, fold(uv.x * 8.), 1.)
}
```

## The rules this enforces

**[Rule 8.4](https://typeshade.dev/reference/rules/8-4/) Test**

A function must not take part in a call cycle, directly or through other functions; the check reads the calls a body writes, and the calls a method call, an accessor and `new` lower to, before any optimisation, so a call in a branch the optimizer would drop is a cycle too.

**[Rule 8.18](https://typeshade.dev/reference/rules/8-18/) Test**

A function whose parameter has a function type, written out (`f: (x: f32) => f32`) or through a type alias of one, takes a function: a function declared at the top of the file or of a namespace, a method, a static method, a constructor, a field that holds a function ([Rule 8.16](https://typeshade.dev/reference/rules/8-16/)) and a local function ([Rule 8.17](https://typeshade.dev/reference/rules/8-17/)).

**[Rule 8.19](https://typeshade.dev/reference/rules/8-19/) Test**

A function that writes no return type returns what its body does, as TypeScript infers it: a function of the file or of a namespace, a local function ([Rule 8.17](https://typeshade.dev/reference/rules/8-17/)), each instance of a generic function ([Rule 8.9](https://typeshade.dev/reference/rules/8-9/)) and of a function that takes a function ([Rule 8.18](https://typeshade.dev/reference/rules/8-18/)), a method, a getter and a field that holds a function ([Rule 8.16](https://typeshade.dev/reference/rules/8-16/)) take the type of their first `return` with a value, the `return`s after it are typed against that type as against a written one, and one with no such `return` returns nothing.

**[Rule 12.6](https://typeshade.dev/reference/rules/12-6/) Test**

A requirement the front end can check must be checked at the front end, in the author’s words, and not left to Tint or a driver.

## See also

- [Functions](https://typeshade.dev/guide/language/functions/)
- [Error codes](https://typeshade.dev/reference/errors/)

## Source

Where the compiler raises this code at commit af54ddad5, one line per file:

- [`src/compiler/ts/lower/function.ts:606`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/lower/function.ts#L606) (4 sites)
- [`src/compiler/ts/recursion.ts:146`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/recursion.ts#L146) (2 sites)
- [`src/language-service/diagnostics.ts:112`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/language-service/diagnostics.ts#L112)
