@nehemy/result-monad
Version:
A simple project to help developers work with Result type
91 lines • 3.74 kB
TypeScript
export declare class Result<T> {
private readonly _value?;
private readonly _error?;
/**
* Represents a result that holds a value.
* @param value - Value of the result.
*/
constructor(value: T);
/**
* Represents a result that holds an error.
* @param error - Error of the result.
*/
constructor(error: Error);
/**
* Creates a failure result from an error instance.
* @param error - The error instance.
* @returns A new failure result containing the error.
*/
static fromError<T>(error: Error): Result<T>;
/**
* Retrieves the value of a result if it exists; otherwise, invokes a factory function to get a value.
* @param factoryFunc - The function invoked when the result contains an error to produce a value.
* @returns The value of the result if it is successful; otherwise, the value produced by the factory function.
*/
orInvoke(factoryFunc: () => T): T;
/**
* Retrieves the value of a result if it exists; otherwise, returns a default value.
* @param defaultValue - The default value to be returned if the result is not successful.
* @returns The value of the result if it is successful; otherwise, the provided default value.
*/
or(defaultValue: T): T;
/**
* Matches a result and calls the appropriate callback based on its success or failure.
* @param onSuccess - Callback function to be called with the value of the result if it is successful.
* @param onFailure - Callback function to be called with the error of the result if it is a failure.
* @returns void
*/
match(onSuccess: (value: T) => void, onFailure: (error: Error) => void): void;
/**
* Applies a transformation function to the value contained in a `successful result`
* instance and returns a new Result with the transformed
* value. If called on a `Failure` instance, it simply
* returns a new Result instance with the same error.
* @param transform - Transformation function.
* @returns - A new Result instance.
*/
map<U>(transform: (value: T) => U): Result<U>;
/**
* Applies a transformation function that takes a value and returns a new Result.
* If called on a `successful result` instance, it applies the transformation
* and returns the resulting Result. If called on a `failed result` instance,
* it simply returns a new Failure instance with the same
* error message.
* @param transform - Transformation function.
* @returns - A new Result instance.
*/
flatMap<U>(transform: (value: T) => Result<U>): Result<U>;
/**
* Retrieves the value of the result.
* @throws An error if the result is not successful.
* @returns The value of the result.
*/
get value(): T;
/**
* Retrieves the error of the result, if any.
* @returns The error of the result, or undefined if the result is successful.
*/
get error(): Error | undefined;
/**
* Checks if the result is successful.
* @returns True if the result is successful; false otherwise.
*/
get isSuccessful(): boolean;
/**
* Checks if the result is not successful (i.e., a failure).
* @returns True if the result is not successful (a failure); false otherwise.
*/
get isNotSuccessful(): boolean;
/**
* Checks if the result has a value.
* @returns True if the result has a value; false otherwise.
*/
get hasValue(): boolean;
/**
* Validates the result and throws an error if it is a failure.
* @throws An error if the result is a failure.
* @returns void
*/
private validate;
}
//# sourceMappingURL=result.d.ts.map