FuncType - v1.21.0
    Preparing search index...

    Interface Reshapeable<T>

    Interface for types that can be reshaped (converted) between different monadic containers. Provides standard conversion methods to transform between Option, Either, List, and Try types.

    // Convert Option to Either
    const opt = Option(5)
    const either = opt.toEither("None value") // Right(5)
    // Convert Either to Option
    const right = Right(10)
    const option = right.toOption() // Some(10)
    // Convert List to Try
    const list = List([1, 2, 3])
    const tryVal = list.toTry() // Success(1) - uses first element
    // Use with Do comprehensions
    const result = Do(function* () {
    const x = yield* $(Option(5))
    const y = yield* $(Right<string, number>(10))
    return x + y
    })

    // Convert to desired type for chaining
    const asOption = result.toOption()
    asOption.map(x => x * 2).orElse(0)
    interface Reshapeable<out T extends Type> {
        toEither<E extends unknown>(leftValue: E): Either<E, T>;
        toList(): List<T>;
        toOption(): Option<T>;
        toTry(): Try<T>;
    }

    Type Parameters

    • out T extends Type

      The type of the value contained in the monad

    Hierarchy (View Summary)

    Index
    • Converts this monad to an Either.

      Conversion rules:

      • Option: Some → Right, None → Left(leftValue)
      • Either: returns self
      • List: non-empty → Right(head), empty → Left(leftValue)
      • Try: Success → Right, Failure → Left(leftValue). Try also accepts a builder (err: Error) => E to thread the underlying Error's context into the Left.

      Type Parameters

      • E extends unknown

      Parameters

      • leftValue: E

        The value to use for the Left case when the source is empty/none/failure

      Returns Either<E, T>

      An Either with the value as Right or the provided leftValue as Left

    • Converts this monad to a List.

      Conversion rules:

      • Option: Some → List([value]), None → List([])
      • Either: Right → List([value]), Left → List([])
      • List: returns self
      • Try: Success → List([value]), Failure → List([])

      Returns List<T>

      A List containing the value(s) if present, empty List otherwise

    • Converts this monad to an Option.

      Conversion rules:

      • Option: returns self
      • Either: Right → Some, Left → None
      • List: non-empty → Some(head), empty → None
      • Try: Success → Some, Failure → None

      Returns Option<T>

      An Option containing the value if present, None otherwise

    • Converts this monad to a Try.

      Conversion rules:

      • Option: Some → Success, None → Failure(Error("None"))
      • Either: Right → Success, Left → Failure(Error(leftValue))
      • List: non-empty → Success(head), empty → Failure(Error("Empty list"))
      • Try: returns self

      Returns Try<T>

      A Try containing Success with the value or Failure with an appropriate error