matchEnum
On this page

matchEnum()

Function in Authoring

Exhaustive integer dispatch over an enumU32: matchEnum(kind, Kind, { Line: () => …, Fill: () => …, … }).

import { matchEnum } from 'typeshade'

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

Syntax

function matchEnum<M extends Record<string, number>, R extends string>(
scrutinee: ReadonlyNode<ScalarKey>,
e: EnumU32<M>,
arms: { readonly [K in keyof M]: () => ReadonlyNode<R> },
): Node<R>

Parameters

scrutinee ReadonlyNode<ScalarKey>
e EnumU32<M>
arms { readonly [K in keyof M]: () => ReadonlyNode<R> }

Return value

Node<R>

Description

Every member must have an arm; omit one and tsc errors, since the arms type is a mapped type over the enum’s keys, so adding a member surfaces every dispatch site that does not handle it. It builds the same matchExpr the hand-written form would: the last-declared member becomes the switch default, so the emitted code is a standard exhaustive switch. Arms are zero-argument functions, called in declaration order.

In the guide

See also

Source

src/core/ir/node.ts, line 2360, at commit 26de7be8

Edit this page Report a problem