FuncType - v0.16.0
    Preparing search index...

    Interface Try<T>

    Base interface for all functype data structures. This provides a standard contract with core functional programming traits.

    // Implementing FunctypeBase for a custom data structure
    class MyContainer<T> implements FunctypeBase<T, "Empty" | "Full"> {
    // Implementation of all required methods...
    }
    interface Try<T> {
        _tag: TypeNames;
        ap: <U>(ff: Try<(value: T) => U>) => Try<U>;
        error: Error | undefined;
        flatMap: <U>(f: (value: T) => Try<U>) => Try<U>;
        flatMapAsync: <U>(f: (value: T) => Promise<Try<U>>) => Promise<Try<U>>;
        fold: <U extends unknown>(
            onFailure: (error: Error) => U,
            onSuccess: (value: T) => U,
        ) => U;
        getOrElse: (defaultValue: T) => T;
        getOrThrow: (error?: Error) => T;
        map: <U>(f: (value: T) => U) => Try<U>;
        orElse: (alternative: Try<T>) => Try<T>;
        orNull: () => T | null;
        orThrow: (error: Error) => T;
        orUndefined: () => T | undefined;
        toEither: <E extends unknown>(leftValue: E) => Either<E, T>;
        toList: () => List<T>;
        toOption: () => Option<T>;
        toString: () => string;
        toTry: () => Try<T>;
        get isEmpty(): boolean;
        get size(): number;
        contains(value: T): boolean;
        count(p: (x: T) => boolean): number;
        doUnwrap(): DoResult<T>;
        exists(p: (a: T) => boolean): boolean;
        find(p: (a: T) => boolean): Option<T>;
        foldLeft<B>(z: B): (op: (b: B, a: T) => B) => B;
        foldRight<B>(z: B): (op: (a: T, b: B) => B) => B;
        forEach(f: (a: T) => void): void;
        isFailure(): this is Try<T> & { _tag: "Failure"; error: Error };
        isSuccess(): this is Try<T> & { _tag: "Success"; error: undefined };
        match<R>(
            patterns: { Failure: (error: Error) => R; Success: (value: T) => R },
        ): R;
        pipe<U extends unknown>(f: (value: T) => U): U;
        reduce(f: (b: T, a: T) => T): T;
        reduceRight(f: (b: T, a: T) => T): T;
        serialize(): SerializationMethods<T>;
        toPromise(): Promise<T>;
        toValue(): { _tag: TypeNames; value: Error | T };
    }

    Type Parameters

    • T

      The type of value contained in the functor

    Hierarchy (View Summary)

    Index

    Properties

    _tag: TypeNames
    ap: <U>(ff: Try<(value: T) => U>) => Try<U>
    error: Error | undefined
    flatMap: <U>(f: (value: T) => Try<U>) => Try<U>
    flatMapAsync: <U>(f: (value: T) => Promise<Try<U>>) => Promise<Try<U>>
    fold: <U extends unknown>(
        onFailure: (error: Error) => U,
        onSuccess: (value: T) => U,
    ) => U

    Pattern matches over the Try, applying onFailure if Failure and onSuccess if Success

    Type Declaration

      • <U extends unknown>(
            onFailure: (error: Error) => U,
            onSuccess: (value: T) => U,
        ): U
      • Type Parameters

        • U extends unknown

        Parameters

        • onFailure: (error: Error) => U

          Function to apply if the Try is Failure

        • onSuccess: (value: T) => U

          Function to apply if the Try is Success

        Returns U

        The result of applying the appropriate function

    getOrElse: (defaultValue: T) => T

    Returns the contained value or a default value

    Type Declaration

      • (defaultValue: T): T
      • Parameters

        • defaultValue: T

          The value to return if extraction fails

        Returns T

        The contained value or defaultValue

    getOrThrow: (error?: Error) => T

    Extract the value or throw an error

    Type Declaration

      • (error?: Error): T
      • Parameters

        • Optionalerror: Error

          Optional custom error to throw. If not provided, uses type-appropriate default error

        Returns T

        The contained value

    The specified error, container's error, or a sensible default

    map: <U>(f: (value: T) => U) => Try<U>
    orElse: (alternative: Try<T>) => Try<T>

    Returns this container if it has a value, otherwise returns the alternative

    Type Declaration

      • (alternative: Try<T>): Try<T>
      • Parameters

        • alternative: Try<T>

          The alternative container

        Returns Try<T>

        This container or the alternative

    orNull: () => T | null

    Returns the contained value or null

    Type Declaration

      • (): T | null
      • Returns T | null

        The contained value or null

    orThrow: (error: Error) => T
    orUndefined: () => T | undefined

    Returns the contained value or undefined

    Type Declaration

      • (): T | undefined
      • Returns T | undefined

        The contained value or undefined

    toEither: <E extends unknown>(leftValue: E) => Either<E, T>

    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(error)

    Type Declaration

      • <E extends unknown>(leftValue: E): Either<E, T>
      • 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

    toList: () => List<T>

    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([])

    Type Declaration

      • (): List<T>
      • Returns List<T>

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

    toOption: () => Option<T>

    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

    Type Declaration

      • (): Option<T>
      • Returns Option<T>

        An Option containing the value if present, None otherwise

    toString: () => string

    Returns a string representation of an object.

    toTry: () => Try<T>

    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

    Type Declaration

      • (): Try<T>
      • Returns Try<T>

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

    Accessors

    • get isEmpty(): boolean

      Returns boolean

    • get size(): number

      Returns number

    Methods

    • Counts elements that satisfy the predicate. For single-value containers: returns 0 or 1 For collections: returns the count of matching elements

      Parameters

      • p: (x: T) => boolean

      Returns number

    • Tests whether any element satisfies the predicate. For single-value containers: tests the single value For collections: returns true if any element matches

      Parameters

      • p: (a: T) => boolean

      Returns boolean

    • Finds the first element that satisfies the predicate. For single-value containers: returns Some(value) if predicate matches, None otherwise For collections: returns the first matching element wrapped in Option

      Parameters

      • p: (a: T) => boolean

      Returns Option<T>

    • Left-associative fold using the provided zero value and operation

      Type Parameters

      • B

      Parameters

      • z: B

        Zero/identity value

      Returns (op: (b: B, a: T) => B) => B

      A function that takes an operation to apply

    • Right-associative fold using the provided zero value and operation

      Type Parameters

      • B

      Parameters

      • z: B

        Zero/identity value

      Returns (op: (a: T, b: B) => B) => B

      A function that takes an operation to apply

    • Applies an effect function to each element. For single-value containers: applies to the value if present For collections: applies to each element

      Parameters

      • f: (a: T) => void

      Returns void

    • Returns this is Try<T> & { _tag: "Failure"; error: Error }

    • Returns this is Try<T> & { _tag: "Success"; error: undefined }

    • Pattern matches over the Try, applying a handler function based on the variant

      Type Parameters

      • R

      Parameters

      • patterns: { Failure: (error: Error) => R; Success: (value: T) => R }

        Object with handler functions for Success and Failure variants

      Returns R

      The result of applying the matching handler function

    • Pipes the value through the provided function

      Type Parameters

      • U extends unknown

        The return type of the function

      Parameters

      • f: (value: T) => U

        The function to apply to the value

      Returns U

      The result of applying the function to the value

    • Converts this container to a Promise

      The behavior depends on the implementing container:

      • Success/Right/Some containers resolve with their value
      • Failure/Left/None containers reject with their error/default error

      Returns Promise<T>

      A Promise that resolves or rejects based on the container's state