@ossedb/patmat - v1.0.0
    Preparing search index...

    Function adt

    • Defines an algebraic data type. Each spec entry is either null (a nullary case, exposed as a frozen singleton value) or a function from constructor arguments to the case's fields.

      Nothing is written to the global scope: all constructors live on the returned object.

      Type Parameters

      • Name extends string
      • Spec extends Record<string, CaseSpec>

      Parameters

      Returns Adt<Name, Spec>

      const Shape = adt('Shape', {
      Circle: (radius: number) => ({ radius }),
      Rect: (w: number, h: number) => ({ w, h }),
      });
      type Shape = VariantOf<typeof Shape>;

      const area = (s: Shape): number =>
      Shape.match(s, {
      Circle: ({ radius }) => Math.PI * radius ** 2,
      Rect: ({ w, h }) => w * h,
      });