FuncType - v2.0.0
    Preparing search index...

    Interface SuccessOf<T>

    A Try that succeeded. Discriminated by _tag: "Success".

    interface SuccessOf<out T> {
        _tag: "Success";
        "[toStringTag]": string;
        error: undefined;
        value: T;
        ap<U>(ff: Try<(value: T) => U>): Try<U>;
        contains(value: T): boolean;
        doUnwrap(): DoResult<T>;
        every(p: (a: T) => boolean): boolean;
        exists(p: (a: T) => boolean): boolean;
        expect(handler: (error: Error) => never): T;
        filterOrElse(
            predicate: (value: T) => boolean,
            onUnsatisfied: (value: T) => Error,
        ): Try<T>;
        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;
        foldAsync<U extends unknown>(
            onFailure: (error: Error) => U | Promise<U>,
            onSuccess: (value: T) => U | Promise<U>,
        ): Promise<U>;
        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;
        getOrElse<T2 extends unknown = never>(defaultValue: () => T | T2): T | T2;
        isFailure(): this is FailureOf<T>;
        isSuccess(): this is SuccessOf<T>;
        map<U>(f: (value: T) => U): Try<U>;
        match<R>(
            patterns: { Failure: (error: Error) => R; Success: (value: T) => R },
        ): R;
        orElse<T2 extends unknown>(alternative: () => Try<T2>): Try<T | T2>;
        orNull(): T | null;
        orThrow(error?: Error | ((error: Error) => Error)): T;
        orUndefined(): T | undefined;
        pipe<U extends unknown>(f: (value: T) => U): U;
        recover<U extends unknown>(f: (error: Error) => U): Try<T | U>;
        recoverWith<U extends unknown>(f: (error: Error) => Try<U>): Try<T | U>;
        serialize(): SerializationMethods<T>;
        toArray(): readonly T[];
        toEither<E extends unknown>(
            leftOrBuilder: E | ((err: Error) => E),
        ): Either<E, T>;
        toJSON(): | { _tag: "Failure"; "@functype": "Try"; error: SerializedError }
        | { _tag: "Success"; "@functype": "Try"; value: T };
        toList(): List<T>;
        toOption(): Option<T>;
        toPromise(): Promise<T>;
        toString(): string;
        toTry(): Try<T>;
        toValue(): { _tag: TypeNames; value: Error | T };
    }

    Type Parameters

    • out T

    Hierarchy (View Summary)

    Index
    _tag: "Success"
    "[toStringTag]": string
    error: undefined

    The error of a Failure; undefined on a Success

    value: T

    The value of a Success; undefined on a Failure

    • Parameters

      • value: T

      Returns boolean

    • Tests whether the success value, if there is one, satisfies the predicate. The dual of exists, and Scala's forall: with no success value it gives true, as [].every(p) does.

      Parameters

      • p: (a: T) => boolean

      Returns boolean

    • Parameters

      • p: (a: T) => boolean

      Returns boolean

    • Returns the success value, or calls the never-returning handler with the Failure's error. Use this when you have a helper like (msg) => fail(msg, 2) that terminates the program — the result type is unconditionally T, so you avoid the TypeScript narrowing trap where if (t.isFailure()) fail(...) fails to narrow t.value when fail is an arrow function typed as (...): never.

      Parameters

      • handler: (error: Error) => never

      Returns T

    • Applies a predicate to the success value. If the predicate fails, short-circuits to a Failure with the error produced by onUnsatisfied. Failure passes through unchanged (predicate not evaluated). Lets you turn a value-level guard into the error channel without writing a manual throw inside a Try(() => ...) body.

      Parameters

      • predicate: (value: T) => boolean

        The predicate to test the success value

      • onUnsatisfied: (value: T) => Error

        Builds the Error for the new Failure when the predicate fails

      Returns Try<T>

      A Success if the predicate holds, a Failure carrying onUnsatisfied(value) otherwise

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

      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

    • Async variant of fold. Accepts sync or async handlers on either branch and always returns a Promise.

      Type Parameters

      • U extends unknown

      Parameters

      • onFailure: (error: Error) => U | Promise<U>
      • onSuccess: (value: T) => U | Promise<U>

      Returns Promise<U>

    • 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

    • Parameters

      • f: (a: T) => void

      Returns void

    • Returns the value, or calls defaultValue on a Failure. The default is lazy: it runs only on a Failure. A default that fits T keeps T, so a literal union is not widened; a default of another type widens the result to T | T2.

      Type Parameters

      • T2 extends unknown = never

      Parameters

      • defaultValue: () => T | T2

      Returns T | T2

    • Type Parameters

      • U

      Parameters

      • f: (value: T) => U

      Returns Try<U>

    • 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

    • Returns this Try if it is a Success, otherwise calls alternative for another Try. The alternative is lazy: it runs only on a Failure.

      Type Parameters

      • T2 extends unknown

      Parameters

      • alternative: () => Try<T2>

      Returns Try<T | T2>

    • Returns the contained value or null

      Returns T | null

      The contained value or null

    • Returns the value, or throws on a Failure. Pass an Error to throw it, or a builder to make one from the failure's error. With no argument, rethrows the original error.

      Parameters

      • Optionalerror: Error | ((error: Error) => Error)

      Returns T

    • Returns the contained value or undefined

      Returns T | undefined

      The contained value or undefined

    • 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

    • Recovers from a Failure by applying a function to the error, returning a new Try. The recovery value may be a wider type; the result is Try<T | U>, matching Scala's recover[U >: T] shape so Try stays covariant in T.

      Type Parameters

      • U extends unknown

      Parameters

      • f: (error: Error) => U

      Returns Try<T | U>

    • Recovers from a Failure by applying a function that returns a new Try. As with recover, the recovery Try may carry a wider type; the result widens accordingly.

      Type Parameters

      • U extends unknown

      Parameters

      • f: (error: Error) => Try<U>

      Returns Try<T | U>

    • Converts to a plain readonly array: [value] for Success, [] for Failure. Symmetric with Try.toList() but skips the List wrapper for code that just wants to feed Array.prototype methods or spread into another array.

      Returns readonly T[]

    • Converts to Either<E, T>. Failure becomes Left(builder(error)) when given a function, or Left(leftValue) when given a value. Success becomes Right(value).

      Prefer the function form to thread the underlying Error's context into the Left:

      Try(() => JSON.parse(text)).toEither((e) => parse failed: ${e.message})

      Type Parameters

      • E extends unknown

      Parameters

      • leftOrBuilder: E | ((err: Error) => E)

      Returns Either<E, T>

    • Custom JSON serialization. Success emits {"@functype":"Try","_tag":"Success","value":T}. Failure emits {"@functype":"Try","_tag":"Failure","error":SerializedError} where SerializedError captures name, message, stack, and the full cause chain — e.name survives round-trip but instanceof SomeError does not.

      Returns
          | { _tag: "Failure"; "@functype": "Try"; error: SerializedError }
          | { _tag: "Success"; "@functype": "Try"; value: 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([])

      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 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

    • Returns a string representation of an object.

      Returns string

    • 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