/** * Type that represents a failable computation. Is parametrized * over the result type and the error type. */ export interface IFailable { /** * Return an object containing the result of * the computation from which the value or error * can be extracted using .isError check. * Useful as an alternative to .match * * @example * ``` * * const failable: IFailable = ...; * if (failable.result.isError) { * // .error can be accessed inside this block * console.log(failable.result.error); * } else { * // .value can be accessed inside this block * console.log(failable.result.value); * } * ``` */ readonly result: IFailableResult; /** * Transform an {@link IFailable} into an {@link IFailable} * by applying the given function to the result value in * case of success. Has no effect in case this is a failure. * @param f Function that transforms a success value. * * @example * ``` * * const numStr: Failable = ...; * const parsed: Failable = numStr.map(parseInt); // or numStr.map(s => parseInt(s)) * ``` */ map(f: (r: Result) => R2): IFailable; /** * Transform the error value of an {@link IFailable} using the * given function. Has no effect if the {@link IFailable} was * a success. * @param f Function for transforming the error value * * @example * ``` * * const result: Failable = ...; * const withErrorCode: Failable = result.mapError(getErrorCode) * ``` */ mapError(f: (e: Error) => E2): IFailable; /** * Pattern match over this IFailable by supplying * a success and failure functions. Both cases * must return a value of type T * @param cases Match cases * * @example * ``` * * const result: Failable = ...; * const num = result.match({ * success: x => x, * failure: err => { * console.log(err); * return 0; * } * }); // num = x if result was successful, otherwise 0 * ``` */ match(cases: IFailableMatchCase): T; /** * Chain another computation to this IFailable that * takes the result value of this IFailable and returns * a new IFailable (possibly of a different type). * The chained computation must be an IFailable whose error * type is a subset of this IFailable's error type. * If not, you can call .mapError on it to convert it's * error into a type compatible with this IFailable. * * This method allows you to chain arbitrary failable computations * dependent on the results of previous ones in the chain that * "short circuit" in case of the first error. * * @param f Function that takes the success value of this * IFailable and returns another IFailable (possibly of another * type) * * @example * ``` * * const computation1: () => Failable = ...; * const computation2: (x: int) => Failable = ...; * const result: Failable = computation1().flatMap(x => computation2(x)) * ``` */ flatMap(f: (r: Result) => IFailable): IFailable; } /** * Discriminated union for an {@link IFailable} result. * value or error can be extracted from it using an * `if (r.isError)` check */ export declare type IFailableResult = { readonly isError: true; readonly error: E; } | { readonly isError: false; readonly value: T; }; /** * Argument type of .match method on an {@link IFailbale}. * It takes an object containing two callbacks; One for * success and failure case. * The value returned by these callbacks should be the * same type. */ export interface IFailableMatchCase { /** * Callback that is run in case of failure. * It is passed the error value of the result. */ failure(e: E): T; /** * Callback that is called in case of success. * It is passed the success value of the result. */ success(v: R): T; } export declare type FailablePromise = Promise>; export declare type FailableAsyncFunctionParams = { success(value: T): Promise>; failure(error: E): Promise>; run(f: IFailable): R; }; export declare type FailableAsyncArg = ((arg: FailableAsyncFunctionParams) => Promise>); /** * Async version of failable that takes a computation that * returns a Promise>. It can be combined with * async/await * * @example * ``` * * const computation1: () => FailablePromise = ...; * const computation2: (x: string) => FailablePromise = ...; * const computation3: (x: number) => Failable = ...; * const computation4 = failableAsync(async ({ run, success, failure }) => { * const str = run(await computation1()); * const num1 = run(await computation2(str)); * * // notice that computation3 returns a non async failable so await isn't required * const num = run(computation3(num1)); * if (num > 10) { * return success(num); * } else { * return failure("Number too small"); * } * }) * ``` */ export declare function failableAsync(f: FailableAsyncArg): Promise>; export declare type FailableArgParams = { /** * Make IFailable from a T * @param value */ success(value: T): IFailable; failure(error: E): IFailable; run(f: IFailable): R; }; export declare type FailableArg = ((arg: FailableArgParams) => IFailable); /** * Creates a failable comutation from a function. * The supplied function receives an object containing * helper functions to create IFailable values. You * need to give generic arguments T and E to it indicating * the success and failure types. * * @param f Failable computation * * @example * ``` * * const computation1: () => Failable = ...; * const computation2: (x: string) => Failable = ...; * const computation3 = failable(({ run, success, failure }) => { * const str = run(computation1()); * const num = run(computation2(str)); * if (num > 10) { * return success(num); * } else { * return failure("Number too small"); * } * }) * ``` */ export declare function failable(f: FailableArg): IFailable; /** * Create an error {@link IFailable} value. * @param err Error value */ export declare function failure(err: E): IFailable; /** * Create a successful {@link IFailable} value * @param value Result value */ export declare function success(value: T): IFailable; /** * Helper type for an async function that * takes Req and returns a {@link FailablePromise}. */ export declare type AsyncFunction = (req: Req) => FailablePromise; /** * Take an array of elements and apply a failable computation to * the array, one element at a time, returning an IFailable of items. * @param arr Array of values of type T * @param f Function that takes an item of type T from the given array * and returns an IFailable. * @returns A failable containing an array of U values wrapped inside * an {@link IFailable} */ export declare function mapMultiple(arr: ReadonlyArray, f: (t: T) => IFailable): IFailable; export declare const mapM: typeof mapMultiple; export declare function isFailableException(e: T): boolean; /** * Object containing static functions for {@link IFailable}. * Anything that isn't an instance method should be added here. */ export declare const Failable: { of: typeof success; success: typeof success; failure: typeof failure; mapM: typeof mapMultiple; mapMultiple: typeof mapMultiple; isFailableException: typeof isFailableException; };