FuncType - v0.8.85
    Preparing search index...

    Variable Match

    Match: <T extends unknown, R extends unknown>(value: T) => Match<T, R> & {
        exhaustive: <T extends string | number | symbol, R extends unknown>(
            cases: Record<T, R>,
        ) => (value: T) => R;
        partial: <T extends string | number | symbol, R extends unknown>(
            cases: Partial<Record<T, R | ((value: T) => R)>>,
        ) => {
            withDefault: (defaultValue: R | ((value: T) => R)) => (value: T) => R;
        };
        withGuards: <T extends unknown, R extends unknown>(
            guards: [(value: T) => boolean, R | ((value: T) => R)][],
        ) => {
            withDefault: (defaultValue: R | ((value: T) => R)) => (value: T) => R;
        };
    }

    Pattern matching utility for type-safe conditional logic

    Type declaration

      • <T extends unknown, R extends unknown>(value: T): Match<T, R>
      • Create a new pattern match expression

        Type Parameters

        • T extends unknown
        • R extends unknown

        Parameters

        • value: T

        Returns Match<T, R>

        const result = Match(httpStatus)
        .case(s => s >= 200 && s < 300, "Success")
        .case(s => s >= 400 && s < 500, "Client Error")
        .case(s => s >= 500, "Server Error")
        .default("Unknown")
    • exhaustive: <T extends string | number | symbol, R extends unknown>(
          cases: Record<T, R>,
      ) => (value: T) => R

      Create a type-safe exhaustive match for union types

      type Status = "pending" | "success" | "error"
      const status: Status = "success"
      const result = Match.exhaustive<Status, string>({
      pending: "Waiting...",
      success: "Done!",
      error: "Failed!"
      })(status)
      // result = "Done!"
      // For function values, wrap in object to prevent execution
      type Operation = "add" | "subtract" | "multiply"
      const ops = Match.exhaustive<Operation, { fn: (a: number, b: number) => number }>({
      add: { fn: (a, b) => a + b },
      subtract: { fn: (a, b) => a - b },
      multiply: { fn: (a, b) => a * b }
      })
      const compute = ops("multiply").fn
      const result = compute(4, 5) // 20
    • partial: <T extends string | number | symbol, R extends unknown>(
          cases: Partial<Record<T, R | ((value: T) => R)>>,
      ) => {
          withDefault: (defaultValue: R | ((value: T) => R)) => (value: T) => R;
      }

      Create a partial match that requires a default

      const httpCode = 404
      const message = Match.partial<number, string>({
      200: "OK",
      201: "Created",
      404: "Not Found",
      500: "Server Error"
      }).withDefault("Unknown Status")(httpCode)
      // message = "Not Found"
      // With function default
      const getMessage = Match.partial<number, string>({
      0: "Zero",
      1: "One",
      2: "Two"
      }).withDefault((n) => `Number: ${n}`)
      getMessage(5) // "Number: 5"
    • withGuards: <T extends unknown, R extends unknown>(
          guards: [(value: T) => boolean, R | ((value: T) => R)][],
      ) => {
          withDefault: (defaultValue: R | ((value: T) => R)) => (value: T) => R;
      }

      Pattern match with guards

      const score = 85
      const grade = Match.withGuards<number, string>([
      [n => n >= 90, "A"],
      [n => n >= 80, "B"],
      [n => n >= 70, "C"],
      [n => n >= 60, "D"]
      ]).withDefault("F")(score)
      // grade = "B"
      // With function results for custom messages
      const age = 25
      const category = Match.withGuards<number, string>([
      [n => n < 13, n => `Child (${n} years)`],
      [n => n < 20, n => `Teenager (${n} years)`],
      [n => n < 60, n => `Adult (${n} years)`],
      [n => n >= 60, n => `Senior (${n} years)`]
      ]).withDefault("Unknown")(age)
      // category = "Adult (25 years)"
    // Basic pattern matching
    const result = Match(value)
    .case(x => x > 0, "positive")
    .case(x => x < 0, "negative")
    .default("zero")
    // Exhaustive matching for union types
    type Color = "red" | "green" | "blue"
    const hex = Match.exhaustive<Color, string>({
    red: "#FF0000",
    green: "#00FF00",
    blue: "#0000FF"
    })(color)
    // Partial matching with default
    const name = Match.partial<number, string>({
    1: "one",
    2: "two",
    3: "three"
    }).withDefault(n => `number ${n}`)(value)