UNPKG

@consolidados/results

Version:
129 lines (125 loc) 5.25 kB
/** * Represents a successful result (`Ok`) that contains a value. * @template T The type of the value contained in this `Ok`. */ declare class Ok<T> implements ResultDefinition<T, never> { private value; /** * Creates a new `Ok` instance with the given value. * @param value The value to wrap in the `Ok` instance. */ constructor(value: T); /** * Checks if this result is an `Ok`. * @returns `true` because this is an `Ok`. */ isOk(): this is Ok<T>; /** * Checks if this result is an `Err`. * @returns `false` because this is an `Ok`. */ isErr(): this is Err<never>; /** * Retrieves the value contained in this `Ok`. * @returns The value contained in this `Ok`. */ unwrap(): T; /** * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value. * @template U The type of the transformed value. * @param fn The transformation function to apply to the value. * @returns A new `Ok` containing the transformed value. */ map<U>(fn: (value: T) => U): ResultDefinition<U, never>; /** * Applies a transformation function that returns a `Result` to the value contained in this `Ok`. * @template U The type of the value in the resulting `Result`. * @param fn The transformation function to apply to the value. * @returns The result of applying the transformation function. */ flatMap<U>(fn: (value: T) => ResultDefinition<U, never>): ResultDefinition<U, never>; /** * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned. * @template U The type of the error (ignored for `Ok`). * @param _fn The mapping function for errors (not used). * @returns The original `Ok` instance. */ mapErr<U extends Error>(_fn: (err: never) => U): ResultDefinition<T, U>; /** * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown. * @throws An error because `unwrapErr` is called on an `Ok`. */ unwrapErr(): never; } type OkType<T> = [T] extends [never] ? never : T; type ErrType<E> = [E] extends [never] ? never : E; /** * Represents the result of an operation that can either succeed (`Ok`) or fail (`Err`). */ type Result<T, E extends Error> = Ok<OkType<T>> | Err<ErrType<E>>; /** * Represents the result methods that must be implemented for success (`Ok`) or failure (`Err`). */ interface ResultDefinition<T = never, E = never> { isOk(): this is Ok<T>; isErr(): this is Err<E extends Error ? E : Error>; unwrap(): T; unwrapErr(): E; map<U>(fn: (value: T) => U): ResultDefinition<U, E>; flatMap<U>(fn: (value: T) => ResultDefinition<U, E>): ResultDefinition<U, E>; mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<T, U>; } /** * Represents a failed result (`Err`) that contains an error value. * @template E The type of the error contained in this `Err`. */ declare class Err<E extends Error> extends Error implements ResultDefinition<never, E> { private error; /** * Creates a new `Err` instance with the given error value. * @param error The error to wrap in the `Err` instance. */ constructor(error: E | string); /** * Checks if this result is an `Ok`. * @returns `false` because this is an `Err`. */ isOk(): this is Ok<never>; /** * Checks if this result is an `Err`. * @returns `true` because this is an `Err`. */ isErr(): this is Err<E extends Error ? E : Error>; /** * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown. * @throws An error because `unwrap` is called on an `Err`. */ unwrap(): never; /** * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned. * @template U The type of the value (ignored for `Err`). * @param _fn The mapping function for values (not used). * @returns The original `Err` instance. */ map<U>(_fn: (value: never) => U): ResultDefinition<never, E>; /** * Maps the error value using a transformation function and returns a new `Result` with the transformed error. * @template U The type of the transformed error. * @param fn The transformation function to apply to the error value. * @returns A new `Err` containing the transformed error. */ mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<never, U>; /** * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`. * @template U The type of the value in the resulting `Result`. * @param _fn The transformation function (ignored in this implementation). * @returns The original `Err` instance. */ flatMap<U>(_fn: (value: never) => ResultDefinition<U, never>): ResultDefinition<never, E>; /** * Retrieves the error value contained in this `Err`. * @returns The error value contained in this `Err`. */ unwrapErr(): E; } export { Err as E, Ok as O, type Result as R };