match-discriminated-union
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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

Package Sidebar

Install

npm i match-discriminated-union

Weekly Downloads

62

Version

1.0.2

License

MIT

Unpacked Size

6.29 kB

Total Files

13

Last publish

Collaborators

  • suchipi