typescript+matchtypescriptmatch
match consumer examples
How a TypeScript app uses @danrabydev/match ^0.3.1: exhaustive Status.match, one ApiResult namespace, peek-then-match, merge, and diagnostics on Error. No _ hatch.
typescriptapp/src/status.ts
import { createMatchable, type MatchableOf } from "@danrabydev/match";
export const Status = createMatchable({
Idle: () => ({}),
Loading: (msg: string) => ({ msg }),
Success: (data: number) => ({ data }),
Error: (err: Error) => ({ err }),
});
export type Status = MatchableOf<typeof Status>;
export function label(status: Status): string {
return Status.match(status, {
Idle: () => "waiting",
Loading: ({ msg }) => `state: ${msg}`,
Success: ({ data }) => `got ${data}`,
Error: ({ err }) => err.message,
});
}
label(Status.Loading("fetching"));
// => "state: fetching"