---
title: "IfChain: class in Authoring, TypeShade API reference"
description: "IfChain, a class in Authoring. The chain object Builder.if and If(/api/if/) return, so .elif(...) and .else(...) read top to bottom in authored order."
url: https://typeshade.dev/api/ifchain/
language: en
---

# `IfChain`

Class in Authoring

The chain object Builder.if and If(/api/if/) return, so .elif(...) and .else(...) read top to bottom in authored order.

`import { IfChain } from 'typeshade/core/ir'`

The signature, the description and the examples come from the compiler's own source at commit af54ddad5.

## Syntax

```ts
class IfChain
```

## Description

The chain object [`Builder.if`](https://typeshade.dev/api/builder/) and [`If`](https://typeshade.dev/api/if/) return, so `.elif(...)` and `.else(...)` read top to bottom in authored order. It is reached only through the call that starts a chain. Its methods extend the same `if` statement in place, pushed once on the first arm, so a chain without a trailing `.else` still emits a valid `if` / `else if` with no `else` block.

## Examples

### Example

```ts
If(p.idx.eq(1), () => {
  pos.assign(vec2(3, -1))
}).elif(p.idx.eq(2), () => {
  pos.assign(vec2(-1, 3))
})
```

## Constructor

**`new IfChain(private readonly parent: Builder, private readonly arms: Array<{ cond: Expr; body: Stmt[] }>, private readonly setElse: (body: Stmt[]) => void)`**

## Instance methods

**`elif` `(cond: ReadonlyNode<'bool'>, body: (b: Builder) => ReadonlyNode | void): IfChain`**

Add an `else if (cond) { body }` arm. Returns the chain. A body that returns a value is rejected with [`SD0115`](https://typeshade.dev/reference/errors/sd0115/), as for [`Builder.if`](https://typeshade.dev/api/builder/).

**`else` `(body: (b: Builder) => ReadonlyNode | void): void`**

Add the `else { body }` block and end the chain. A body that returns a value is rejected with [`SD0115`](https://typeshade.dev/reference/errors/sd0115/), as for [`Builder.if`](https://typeshade.dev/api/builder/).

## See also

- [`Builder`](https://typeshade.dev/api/builder/)
- [`If`](https://typeshade.dev/api/if/)

## Source

[`src/core/ir/builder.ts`, line 355](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/ir/builder.ts#L355), at commit af54ddad5
