FuncType - v0.8.85
    Preparing search index...

    Variable Cond

    Cond: <T extends unknown>() => Cond<T> & {
        lazy: <T extends unknown>() => LazyCondChain<T>;
        match: <T extends string | number | symbol>(
            value: T,
        ) => <R extends unknown>(cases: Record<T, R | (() => R)>) => R;
        of: <T extends unknown>() => Cond<T>;
    }

    Conditional expression builder for functional if/else chains

    Type declaration

      • <T extends unknown>(): Cond<T>
      • Create a new conditional expression

        Type Parameters

        • T extends unknown

        Returns Cond<T>

        const message = Cond<string>()
        .when(isError, "Error occurred")
        .when(isWarning, "Warning")
        .else("All good")
    • lazy: <T extends unknown>() => LazyCondChain<T>

      Create a lazy conditional that defers evaluation

      // Only evaluates conditions and values when needed
      const getMessage = Cond.lazy<string>()
      .when(() => isError(), () => computeErrorMessage())
      .when(() => isWarning(), () => computeWarningMessage())
      .else(() => "Success")
      // Complex conditional with expensive checks
      const result = Cond.lazy<Action>()
      .when(
      () => user.role === "admin" && checkAdminPermissions(),
      () => ({ type: "admin", permissions: loadAdminPermissions() })
      )
      .when(
      () => user.role === "user" && user.isActive,
      () => ({ type: "user", permissions: loadUserPermissions() })
      )
      .else(() => ({ type: "guest", permissions: [] }))
    • match: <T extends string | number | symbol>(
          value: T,
      ) => <R extends unknown>(cases: Record<T, R | (() => R)>) => R

      Pattern matching helper that ensures exhaustiveness

      type Status = "pending" | "success" | "error"
      const status: Status = "success"
      const result = Cond.match(status)({
      "pending": "Waiting...",
      "success": "Done!",
      "error": "Failed!"
      })
      // result = "Done!"
      // With function values
      const action = "compute"
      const result = Cond.match(action)({
      "compute": () => expensiveComputation(),
      "cache": () => getCachedValue(),
      "skip": () => defaultValue
      })
    • of: <T extends unknown>() => Cond<T>

      Create a conditional expression that must end with else

      const x = 7
      const result = Cond.of<string>()
      .when(x > 10, "large")
      .elseWhen(x > 5, "medium")
      .else("small")
      // result = "medium"
      // With lazy evaluation
      const discount = Cond.of<number>()
      .when(isPremium, () => calculatePremiumDiscount())
      .when(isLoyal, () => calculateLoyaltyDiscount())
      .else(0)
    // Basic usage
    const size = Cond.of<string>()
    .when(value > 100, "large")
    .elseWhen(value > 50, "medium")
    .else("small")
    // Pattern matching
    const message = Cond.match(errorCode)({
    404: "Not Found",
    500: "Server Error",
    200: "OK"
    })
    // Lazy evaluation
    const result = Cond.lazy<string>()
    .when(() => checkCondition1(), () => "Result 1")
    .when(() => checkCondition2(), () => "Result 2")
    .else(() => "Default")