githubEdit

Pattern Matching

The pattern matching module provides exhaustive branching helpers for unions and tagged unions.

Exports

  • exhaustive: exhaustive matching over literal unions and tagged unions.

  • exhaustive.tag: exhaustive matching using an explicit tag key.

  • corrupt: helper for unreachable/default branches in switch statements.

exhaustive

Use exhaustive when all variants must be handled.

import { exhaustive } from 'funkcia/pattern-matching';

type State = 'IDLE' | 'LOADING' | 'SUCCESS' | 'ERROR';

function message(state: State) {
  return exhaustive(state, {
    IDLE: () => 'Idle',
    LOADING: () => 'Loading',
    SUCCESS: () => 'Success',
    ERROR: () => 'Error',
  });
}

Tagged unions with _tag

When the union has a _tag field, exhaustive matches by _tag automatically.

Fallback case

You can provide _ as a runtime fallback.

exhaustive.tag

Use exhaustive.tag when the discriminant key is not _tag.

corrupt

Use corrupt in switch defaults to guard impossible branches.

Last updated

Was this helpful?