Skip to content

TypeScript pattern match function for any discriminated union type

License

Notifications You must be signed in to change notification settings

suchipi/match-discriminated-union

Repository files navigation

match-discriminated-union

A simple match function that, provided an instance of a tagged union and its discriminating key, runs the corresponding match handler function and returns its result.

TypeScript types are included which are designed to be flexible and support your existing types. The TypeScript types also require that you either specify handler functions for every variant of the union, or include a "default" case (via the key _).

Usage example

import { match } from "match-discriminated-union";

// An example of a user-defined type to match against
type PendingData<T> = {
  id: "loading",
  percent: number
} | {
  id: "unstarted"
} | {
  id: "complete",
  data: T
} | {
  id "errored",
  error: Error
};

function logProgress(pendingData: PendingData<any>) {
  const message = match(pendingData, "id", {
    loading: ({ percent }) => `Loading (${percent}%)...`,
    unstarted: () => `Waiting...`,
    complete: () => `Completed!`,
    errored: ({ error }) => `Failed: ${error}`,
  });
  console.log(message);
}

function getPercent(pendingData: PendingData<any>) {
  return match(pendingData, "id", {
    loading: ({ percent }) => percent,
    // This '_' property is the default handler for any unspecified members of the union.
    _: () => 0,
  });
}

Previous Work

  • safety-match - Similar goals to this library, but its design couldn't support generics very well.

License

MIT

About

TypeScript pattern match function for any discriminated union type

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published