Skip to content

Pattern matching

Goal: Replace fragile switch with exhaustive match.

match is an expression — every arm produces a value. Missing variants are compile errors (SJS-E007).

Example

type Status = Active | Paused | Done

function label(s: Status): string {
  return match s {
    Active => "active",
    Paused => "paused",
    Done => "done",
  }
}

Open in playground

Key takeaways

  • Arms are separated by commas.
  • Exhaustiveness is enforced at compile time.
  • match works on sum types, not arbitrary strings.

Next: Sum types

Documentation