---
title: "matchExpr(): function in Authoring, TypeShade API reference"
description: "matchExpr, a function in Authoring. A typed multi-arm dispatch over a scalar scrutinee, the expression form of switch (scrutinee) { case v0: r0; …; default…"
url: https://typeshade.dev/api/matchexpr/
language: en
---

# `matchExpr()`

Function in Authoring

A typed multi-arm dispatch over a scalar scrutinee, the expression form of switch (scrutinee) { case v0: r0; …; default: dflt }.

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

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

## Syntax

```ts
function matchExpr<S extends ScalarKey, R extends string>(
  scrutinee: ReadonlyNode<S>,
  cases: ReadonlyArray<
    readonly [caseValue: number, value: ReadonlyNode<R> | (() => ReadonlyNode<R>)]
  >,
  default_: ReadonlyNode<R> | (() => ReadonlyNode<R>),
): Node<R>
```

## Parameters

**`scrutinee` `ReadonlyNode<S>`**

**`cases` `ReadonlyArray< readonly [caseValue: number, value: ReadonlyNode<R> | (() => ReadonlyNode<R>)] >`**

**`default_` `ReadonlyNode<R> | (() => ReadonlyNode<R>)`**

## Return value

`Node<R>`

## Exceptions

**`TypeShadeError`**

[`SD0011`](https://typeshade.dev/reference/errors/sd0011/) when an arm’s type differs from the default’s.

## Description

Each arm pairs an integer case value with its result, given either as a node or as a zero-argument function that builds one; the default takes the same two forms. Before emit, every `matchExpr` in a function body is rewritten into a `var` slot and a `switch` statement that writes each arm’s value into it. A match with ten or more arms casts a non-integer scrutinee to `i32`, since a WGSL `switch` is integer-only.

Every arm’s type must equal the default’s. The shared `R extends string` bound rejects most mismatches at `tsc`; one that reaches the runtime throws.

## Examples

### Example

```ts
import { fn, matchExpr, f32, u32T } from 'typeshade/core/ir'

const width = fn('width', { kind: u32T }, ({ kind }) =>
  matchExpr(kind, [[0, f32(1)], [1, () => f32(2)]], f32(0.5)),
)
```

## See also

- [`matchEnum`](https://typeshade.dev/api/matchenum/)
- [`enumU32`](https://typeshade.dev/api/enumu32/)

## Source

[`src/core/ir/node.ts`, line 2038](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/ir/node.ts#L2038), at commit af54ddad5
