type Curried$3 = (a: Result) => U; declare class WrappedResultError extends Error { originalError: any; constructor(message: string, originalError: any); } type UnderlyingValue = { type: "value"; value: T; }; type UnderlyingError = { type: "error"; error: any; }; type Underlying = UnderlyingValue | UnderlyingError; declare class Result { private value; constructor(value: Underlying); /** * Gets the underlying value. Note that you should check whether the value is Ok before doing this, or else a @see WrappedResultError may be thrown. */ getValue(): T; /** * Gets the underlying error. Note that you should check whether the value is Error before doing this, or else a @see WrappedResultError may be thrown. */ getError(): any; /** * Checks whether the value is Error. */ isError(): boolean; /** * Checks whether the value is Ok. */ isOk(): boolean; /** * Maps the Ok value to another value. Will only run if the value is Ok. */ map(mapper: (arg: T) => U): Result; /** * Maps the Error value back to an OK value. Will only run if the value is Error. */ mapError(mapper: (arg: U) => T): Result; /** * Binds the value to the result returned by the @param mapper function. Will only run if the value is Ok. */ bind(mapper: (arg: T) => Result): Result; /** * Binds the result to the one returned by the @param mapper function. Will only run if the value is Error. */ bindError(mapper: (error: any) => Result): Result; /** * Passes the value to the given @param fn function where it can then be used for work that doesn't return a value. Will only run if the value is Ok. */ iter(fn: (arg: T) => void): Result; /** * Executes the given @param fn function when the result is Error. */ iterError(fn: (arg: any) => void): Result; /** * Returns the @param defaultValue if the Result is Error, else returns the Ok value. * @example * console.log(Result.ofValue(5).defaultValue(10)) // 5 * console.log(Result.ofError(new Error("Test error")).defaultValue(10)) // 10 */ defaultValue(defaultValue: T): T; /** * Wraps the @param value in an Ok Result. */ static ofValue(value: T): Result; /** * Wraps the @param error in an Error Result. */ static ofError(error: any): Result; /** * Takes the given promise or function that returns a promise and wraps it in a try/catch. Returns Result.ofError if it throws an error. */ static ofPromise(promise: Promise | (() => Promise)): Promise>; /** * Takes the given function and executes it, returning Result.ofError if it throws an error. */ static ofFunction(computation: (() => T)): Result; /** * Gets the underlying value of the Result. Note that you should check whether the value is Ok before doing this, or else a @see WrappedResultError may be thrown. */ static getValue(result: Result): T; /** * Gets the underlying error of the Result. Note that you should check whether the value is Error before doing this, or else a @see WrappedResultError may be thrown. */ static getError(result: Result): any; /** * Checks whether the Result is Error. */ static isError(result: Result): boolean; /** * Checks whether the Result is Ok. */ static isOk(result: Result): boolean; /** * Returns a curried function that will map the Ok value to another value. Will only run if the value is Ok. */ static map(mapper: (arg: T) => U): Curried$3>; /** * Returns a curried function that will map the Error value back to an OK value. Will only run if the value is Error. */ static mapError(mapper: (arg: any) => T): Curried$3>; /** * Returns a curried function that will bind the value to the result returned by the @param mapper function. Will only run if the value is Ok. */ static bind(mapper: (arg: T) => Result): Curried$3>; /** * Returns a curried function that will bind the result to the one returned by the @param mapper function. Will only run if the value is Error. */ static bindError(mapper: (arg: any) => Result): Curried$3>; /** * Returns a curried function that will pass the value to the given @param fn function where it can then be used for work that doesn't return a value. Will only run if the value is Ok. */ static iter(fn: (arg: T) => void): Curried$3>; /** * Returns a curried function that will execute the given @param fn function when the result is Error. */ static iterError(fn: (arg: any) => void): Curried$3>; } type Curried$2 = (a: Async) => U; /** * A monad which wraps a promise and provides convenient utility methods for working with that promise. */ declare class Async { private value; constructor(value: Promise); /** * Gets the underlying promise, which can then be awaited. */ get(): Promise; /** * Gets the underlying promise, wrapped in a Result. This is just a shortcut for `Result.ofPromise(Async.get())` */ getResult(): Promise>; /** * Maps the value of the promise to another value. */ map(mapper: (arg: T) => U): Async; /** * Binds the value of the promise to the promise returned by the @param mapper function. */ bind(mapper: (arg: T) => Async): Async; /** * Passes the value to the given @param fn function where it can then be used for work that doesn't require returning a value. */ iter(fn: (arg: T) => void): Async; /** * Creates an Async monad from the promise. */ static ofPromise(promise: Promise): Async; /** * Wraps a non-promise value in an Async monad. */ static wrap(value: T): Async; /** * Gets the underlying promise, which can then be awaited. */ static get(a: Async): Promise; /** * Gets the underlying promise, wrapped in a Result. This is just a shortcut for `Result.ofPromise(Async.get())` */ static getResult(a: Async): Promise>; /** * Returns a curried function that will map the value of the promise to another value. */ static map(mapper: (arg: T) => U): Curried$2>; /** * Returns a curried function that will bind the value of the promise to the promise returned by the @param mapper function. */ static bind(mapper: (arg: T) => Async): Curried$2>; /** * Returns a curried function that will pass the value to the given @param fn function where it can then be used for work that doesn't require returning a value. */ static iter(fn: (arg: T) => void): Curried$2>; } type Curried$1 = (a: AsyncResult) => U; declare class AsyncResult { private value; constructor(value: Promise>); /** * Gets the underlying promise, which can then be awaited to return a @see Result */ get(): Promise>; /** * Maps the OK value to another value. Will only run if the value is OK. */ map(fn: (arg: T) => U): AsyncResult; /** * Maps the Error value back to an OK value. Will only run if the value is Error. */ mapError(fn: (arg: any) => T): AsyncResult; /** * Binds the OK value to the AsyncResult returned by the @param fn function. Will only run if the value is OK. */ bind(fn: (arg: T) => AsyncResult): AsyncResult; /** * Binds the Error value to the AsyncReuslt returned by the @param fn function. Will only run if the value is Error. */ bindError(fn: (arg: any) => AsyncResult): AsyncResult; /** * Passes the OK value to the given @param fn function. Will only run if the value is OK. */ iter(fn: (arg: T) => void): AsyncResult; /** * Passes the Error value to the given @param fn function. Will only run if the value is Error. */ iterError(fn: (arg: any) => void): AsyncResult; /** * Wraps a value, promise or result in an AsyncResult monad. */ static wrap(value: T | Result | Promise>): AsyncResult; /** * Gets the underlying promise, which can then be awaited. */ static get(a: AsyncResult): ReturnType; /** * Returns a curried function that will map the OK value to another value. Will only run if the value is OK. */ static map(fn: (arg: T) => U): Curried$1>; /** * Returns a curried function that will map the Error value back to an OK value. Will only run if the value is Error. */ static mapError(fn: (arg: any) => T): Curried$1>; /** * Returns a curried function that will bind the OK value to the AsyncResult returned by the @param fn function. Will only run if the value is OK. */ static bind(fn: (arg: T) => AsyncResult): Curried$1>; /** * Returns a curried function that will bind the Error value to the AsyncResult returned by the @param fn function. Will only run if the value is Error. */ static bindError(fn: (arg: any) => AsyncResult): Curried$1>; /** * Returns a curried function that will pass the OK value to the given @param fn function. Will only run if the value is OK. */ static iter(fn: (arg: T) => void): Curried$1>; /** * Returns a curried function that will pass the Error value to the given @param fn function. Will only run if the value is Error. */ static iterError(fn: (arg: any) => void): Curried$1>; } /** * A simple function that wraps logic in a computation "block". This is conceptually similar to, and 100% inspired by, the "let" blocks of F#. It's just a simple way to compute the value of a variable using other temporary variables without polluting your scope with those temporary variables, and while keeping the final value immutable (`const`). * Example: * ```ts const order = await compute(async () => { const displayId = await database.getDisplayIdAsync(); const order = { ...baseOrder, user_id: req.user_id, display_id: displayId } const result = await database.createAsync(order); return { ...order, _id: result.id, _rev: result.rev } }) ``` * In this example, order is only declared once, and the temporary variables that only it uses (displayId, result) are wrapped up neatly in the computation. Here's the same example without wrapping in a computation: * ```ts const displayId = await database.getDisplayIdAsync(); let order = { ...baseOrder, user_id: req.user_id, display_id: displayId } const result = await database.createAsync(order); order = { ...order, _id: result.id, _rev: result.rev } ``` * In this example, order is mutable by default (because its value needs to be reassigned after getting the `result`), and the temporary variables that only it uses are polluting the scope of the rest of the function. */ declare function compute(block: () => T): T; type Curried = (option: Option) => U; declare class Option { private readonly value?; constructor(value?: T | null | undefined); /** * Checks whether the value is some. */ isSome: () => boolean; /** * Checks whether the value is none. */ isNone: () => boolean; /** * Gets the underlying value. You *must* check whether the value is some or none before getting, as using this on a value that is none **will** throw an error. */ get(): T; /** * Maps the value to another value. Will only run if the value is some. */ map(mapper: (arg: T) => U): Option; /** * Binds the value to another option. Will only run if the value is some. */ bind(mapper: (arg: T) => Option): Option; /** * Passes the value to the given @param fn function where it can the be used for operations that don't require a value to be returned. */ iter(fn: (arg: T) => void): Option; /** * Executes the given @param fn function when the the option has no value. */ iterNone(fn: () => void): Option; /** * Returns the @param defaultValue if the option is none, else returns the option's value. */ defaultValue(defaultValue: T): T; /** * Executes the @param fn function if the value is none and returns the result, else returns the option value itself. */ defaultWith(fn: () => T): T; /** * Wraps the @param value in an option. */ static ofSome(value: T): Option; /** * Creates an option with no value. */ static ofNone(): Option; /** * Checks whether the value is some. */ static isSome(option: Option): boolean; /** * Checks whether the value is none. */ static isNone(option: Option): boolean; /** * Gets the underlying value. You *must* check whether the value is some or none before getting, as using this on a value that is none **will** throw an error. */ static get(option: Option): T; /** * Returns a curried function that will map the value to another value. Will only run if the value is some. */ static map(mapper: (arg: T) => U): Curried>; /** * Returns a curried function that will bind the value to another option. Will only run if the value is some. */ static bind(mapper: (arg: T) => Option): Curried>; /** * Returns a curried function that will pass the value to the given @param fn function where it can the be used for operations that don't require a value to be returned. */ static iter(fn: (arg: T) => void): Curried>; /** * Returns a curried function that executes the given @param fn function when the the option has no value. */ static iterNone(fn: () => void): Curried>; /** * Returns a curried function that will return the @param defaultValue if the option is none, else returns the option's value. */ static defaultValue(defaultValue: T): Curried; /** * Returns a curried function that will execute the @param fn function if the value is none and returns the result, else returns the option's value itself. */ static defaultWith(fn: () => T): Curried; } interface IPipe { readonly value: () => T; chain(fn: (x: T) => R): IPipe; } /** * A simple function chain that pipes the result of the last function to the next function. Since custom operators are impossible, the `pipe` function instead uses `.chain` and `.value`. * Note that the functions are executed as they're chained, not when the value is retrieved. */ declare function pipe(val: T): IPipe; export { Async, AsyncResult, IPipe, Option, Result, WrappedResultError, compute, pipe };