---
title: "If(): function in Authoring, TypeShade API reference"
description: "If, a function in Authoring. Author if (cond) { body } over the innermost active scope."
url: https://typeshade.dev/api/if/
language: en
---

# `If()`

Function in Authoring

Author if (cond) { body } over the innermost active scope.

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

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

## Syntax

```ts
const If: (
  cond: ReadonlyNode<"bool">,
  body: () => ReadonlyNode | void,
) => IfChain
```

## Parameters

**`cond` `ReadonlyNode<'bool'>`**

the branch condition.

**`body` `() => ReadonlyNode | void`**

the statements of the branch, authored through the ambient functions.

## Return value

`IfChain`

the chain, for `.elif` and `.else`.

## Exceptions

**[`SD0115`](https://typeshade.dev/reference/errors/sd0115/)**

[`SD0115`](https://typeshade.dev/reference/errors/sd0115/) when the body returns a value.

## Description

Chain `.elif(c, () => …)` and `.else(() => …)` on the returned [`IfChain`](https://typeshade.dev/api/ifchain/). A native `return` inside the body callback only exits that closure; for an early return from the branch write [`Return`](https://typeshade.dev/api/return/) or [`ReturnIf`](https://typeshade.dev/api/returnif/).

Because that value goes nowhere, a body that returns one is rejected with [`SD0115`](https://typeshade.dev/reference/errors/sd0115/) instead of being accepted as an empty branch: `If(c, () => f32(1))` differs from `If(c, () => Return(f32(1)))` by three characters and by everything else. For a value, reach for [`when`](https://typeshade.dev/api/when/).

## Examples

### Example

```ts
If(d.lt(0.001), () => {
  Break()
}).else(() => {
  t.assign(t.add(d))
})
```

## See also

- [`IfChain`](https://typeshade.dev/api/ifchain/)
- [`Return`](https://typeshade.dev/api/return/)
- [`ReturnIf`](https://typeshade.dev/api/returnif/)
- [`when`](https://typeshade.dev/api/when/)

## Source

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