@tusss/ood
    Preparing search index...

    Class Result<T>

    Represents the outcome of an operation, which can be either a success containing data or a failure containing an error.

    Example demonstrating creating and serializing successful and failed results

    // 1. Successful result
    const success = new Result({ data: "Hello World" });
    console.log(success.ok); // true
    console.log(success.data); // "Hello World"
    console.log(success.toStruct()); // { data: "Hello World", ok: true }

    // 2. Failed result
    const failure = new Result({ error: new ErrorBase("NOT_FOUND", "User not found") });
    console.log(failure.ok); // false
    console.log(failure.error?.message); // "User not found"

    Type Parameters

    • T = never

      The type of the data returned in a successful result. Defaults to never.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    Accessors

    Methods

    Constructors

    • Creates a new Result instance.

      Type Parameters

      • T = never

        The type of the data returned in a successful result. Defaults to never.

      Parameters

      • props: { data?: T; error?: ErrorBase }

        The initialization properties.

        • Optionaldata?: T

          The data payload.

        • Optionalerror?: ErrorBase

          The error payload.

      Returns Result<T>

    Properties

    data: T

    The data payload of a successful result.

    error?: ErrorBase

    The error object of a failed result.

    Accessors

    • get ok(): boolean

      Indicates whether the result represents a successful operation.

      Returns boolean

      true if there is no error; otherwise false.

    Methods

    • Implements the JSON.stringify() behavior.

      It uses toStruct to get the plain object structure under the hood.

      It is encouraged to use toStruct instead of this method because it provides better clarity and expressiveness about the data structure being serialized. This method is provided only to support the JSON.stringify() serialization.

      Returns ClassProperties<TData>

      A plain object structure containing the model's serializable properties.