import type { Option } from "./option"; import { ResultClass } from "./result-impl"; /** * @ignore * @internal */ export type ResultOk = { /** @ignore */ readonly result: true; /** * If {@link Result.isOk}() is true, this is the contained value of type * `A`. If {@link Result.isErr}() is true, this is the error value of type * `E`. */ readonly value: A; }; /** * @ignore * @internal */ export type ResultErr = { readonly result: false; readonly value: E; }; /** * A value which can be of either type `A` or type `E`. * * This is normally used as a return value for operations which can fail: `E` is * short for `Error`. `A` can be thought of as the success value, and `E` is the * error value. This is reflected in their constructors: to construct a success * value, you call {@link Ok}, and to construct an error value, you call * {@link Err}. * * You can also use the {@link Result.await} function to convert a * {@link Promise} that could throw an exception into a {@link Promise} that * won't throw but instead returns a {@link Result} containing either the error * or the expected result, or the {@link Result.try} function to run a function * and catch any exceptions into a {@link Result} return value. * * @template A The type of the success value. * @template E The type of the error value. * * @interface * * @example * function processResult(result: Result): void { * if (result.isOk()) { * console.info(result.value); * } else { * console.error(result.value.message); * } * } * * processResult(Ok("Hello Joe!")); * processResult(Err(new Error("Get Robert to fix the bug!"))); */ export type Result = (ResultOk | ResultErr) & IResult; /** @ignore */ export interface IResult { /** * Test if the {@link Result} is {@link Ok}. */ isOk(): this is ResultOk; /** * Test if the {@link Result} is {@link Err}. */ isErr(): this is ResultErr; /** * Assert that the {@link Result} is {@link Ok}. * * Throws a {@link TypeError} if it isn't. */ assertOk(): asserts this is ResultOk; /** * Assert that the {@link Result} is {@link Err}. * * Throws a {@link TypeError} if it isn't. */ assertErr(): asserts this is ResultErr; /** * The `match` function takes two callbacks, one for each possible state of * the {@link Result}, and calls the one that matches the actual state. */ match(onOk: (value: A) => B, onErr: (value: E) => B): B; /** * Call the provided function with the contained value if the {@link Result} is {@link Ok}. */ ifOk(onOk: (value: A) => void): void; /** * Call the provided function with the contained value if the {@link Result} is {@link Err}. */ ifErr(onErr: (value: E) => void): void; /** * If the {@link Result} is {@link Ok}, call the provided function with the * contained value and return the result of the function, which must be * another {@link Result}. If the {@link Result} is {@link Err}, the * {@link Err} is returned unchanged and the function is never called. * * This is the monadic bind function, for those who celebrate. */ chain(f: (value: A) => Result): Result; /** * If the {@link Result} is {@link Err}, call the provided function and return its * result, which must be another {@link Result}. If the {@link Result} is {@link Ok}, * the {@link Ok} is returned unchanged and the function is never called. */ chainErr(f: (value: E) => Result): Result; /** * Transform the {@link Result} as in {@link Result.chain} using one of the * two provided functions, according to whether the {@link Result} is * {@link Ok} or {@link Err}. * * You may notice this is just {@link Result.match} with stricter types, to * conform to the monadic bind interface. */ bichain( ok: (okValue: A) => Result, err: (errValue: E) => Result ): Result; /** * If the {@link Result} is {@link Ok}, transform its contained value using the provided function. */ map(f: (value: A) => B): Result; /** * If the {@link Result} is {@link Err}, transform its contained value using the provided function. */ mapErr(f: (value: E) => F): Result; /** * Transform the contained value using one of the provided functions `ok` * and `err`, according to whether the {@link Result} is {@link Ok} or * {@link Err}. */ bimap(ok: (okValue: A) => B, err: (errValue: E) => F): Result; /** * Given another {@link Result} containing a function from `A` to `B`: * * If both {@link Result}s are {@link Ok}, call the function with the * success value of this {@link Result} and return a `Result` * containing what the function returns. * * If the result in `f` is {@link Err}, return that instead. * * If the result in `f` is {@link Ok} but the original result is * {@link Err}, return the original error {@link Result} unchanged. */ ap(f: Result<(a: A) => B, E>): Result; /** * If the {@link Result} is {@link Ok}, return the provided {@link Result}, * otherwise return the original result (which would be an {@link Err}) cast * to `Result`. */ and(result: Result): Result; /** * If the {@link Result} is {@link Err}, return the provided {@link Result}, otherwise * return the original {@link Result} (which would be an {@link Ok}). */ or(result: Result): Result; /** * Return the value contained in the {@link Result} if it's {@link Ok}, or return `defaultValue` otherwise. */ getOr(defaultValue: A): A; /** * Return the value contained in the {@link Result} if it's {@link Ok}, or call the provided function and return its result otherwise. */ getOrElse(f: () => A): A; /** * Converts a {@link Result} into an optional value of `A`, discarding any error value and returning `undefined` in place of the error. */ unwrap(): A | undefined; /** * Converts a {@link Result} into an optional value of `E`, discarding any success value and returning `undefined` in place of the `A` value. */ unwrapErr(): E | undefined; /** * Converts a {@link Result} into a value of `A` if it contains one. If it * contains a value of `E`, throw that as an exception. */ unwrapExact(): A; /** * Converts a {@link Result} into an {@link Option} of the success value, discarding any error value. */ ok(): Option; /** * Converts a {@link Result} into an {@link Option} of the error value, discarding any success value. */ err(): Option; /** * Test if the {@link Result} is an {@link Err} containing a * {@link DOMException} caused by an * {@link AbortController} aborting. */ isAborted(): this is ResultErr; /** * Convert a {@link Result} into a JSON structure for serialisation. */ toJSON(): { result: boolean; value: A | E }; } function awaitImpl(m: Promise): Promise>; function awaitImpl(m: PromiseLike): PromiseLike>; function awaitImpl(m: PromiseLike): PromiseLike> { return m.then(Ok, Err); } /** * Static methods on the {@link Result} object. */ export const Result = { /** * Convert a {@link Promise} returning `A` into a {@link Promise} returning a {@link Result} of either `A` or the `Error` type. * The new {@link Promise} always succeeds, reflecting an error condition in the `Result` instead of the failure callback. * * @example * const fetchResult = await Result.await(fetch("https://example.com/example.txt")); * if (fetchResult.isErr()) { * console.error(fetchResult.value.message); * } */ await: awaitImpl, /** * Run a function and return its result as an {@link Ok} if it didn't throw any * exceptions, or an {@link Err} containing the thrown exception. * * @example * const tryFn: Result = Result.try(() => { * throw new Error("fix the bug!"); * }); * * if (tryFn.isErr()) { * console.error(tryFn.value.message); * } */ try(f: () => A): Result { try { return Ok(f()); } catch (e) { return Err(e as E); } }, /** * Turn a function that returns `A` and potentially throws `E` into a function that * catches `E` if thrown and returns `Result`. * * Beware that TypeScript's type inference isn't currently very good at * this, so you should explicitly provide the target function signature when * using this function, or you're likely to end up with a `Result` * instead of the expected `Result`. * * @example * function div(n: number, by: number): number { * if (by === 0) { * throw new RangeError("division by zero"); * } * return n / by; * } * const liftedDiv: (n: number, by: number) => Result = * Result.lift(div); */ // deno-lint-ignore no-explicit-any lift) => A, E = unknown>( fn: F ): (...args: Parameters) => Result { return (...args) => Result.try(() => fn(...args)); }, /** * Create a {@link Result} from the output of {@link Result.toJSON}. */ fromJSON(doc: { result: boolean; value: A | E }): Result { return doc.result ? Ok(doc.value as A) : Err(doc.value as E); }, /** * Test whether an unknown value is a {@link Result}. * * @example * function assertResult(value: unknown): void { * if (Result.is(value)) { * value.assertOk(); * } * } */ is(value: unknown): value is Result { return value instanceof ResultClass; }, /** * The class constructor of {@link Result}s. You should never use this to * construct {@link Result}s directly, preferring instead {@link Ok} and * {@link Err}. It's exposed for use in `instanceof` checks, though * calling {@link Result.is} to decide resultiness is preferred. * * @example * import { expect } from "chai"; * expect(Ok("stop dots")).to.be.an.instanceof(Result.Class); */ Class: ResultClass, }; /** * Construct a {@link Result} with a success value of `A`. * * @example * const systemWorking: Result = Ok("Seems to be!"); */ // deno-lint-ignore no-explicit-any export function Ok(a: A): Result { const o = Object.create(ResultClass.prototype); o.result = true; o.value = a; return o; } /** * Construct a {@link Result} with an error value of `E`. * * @example * const systemWorking: Result = Err("System down!"); */ // deno-lint-ignore no-explicit-any export function Err(e: E): Result { const o = Object.create(ResultClass.prototype); o.result = false; o.value = e; return o; } /** * Extract the type of the success value from a {@link Result} type. * * @example * type VoteResult = Result<"yes" | "no", Error>; * type VoteOk = OkType; // "yes" | "no" */ // deno-lint-ignore no-explicit-any export type OkType = T extends Result ? A : never; /** * Extract the type of the error value from a {@link Result} type. * * @example * type VoteResult = Result<"yes" | "no", Error>; * type VoteErr = ErrType; // Error */ // deno-lint-ignore no-explicit-any export type ErrType = T extends Result ? E : never;