import { type Option } from "./option"; import { AsyncResult } from "./async_result"; import * as symbols from "./symbols"; export type ResultMatch = { Ok: (value: T) => A; Err: (error: E) => B; }; export type ResultMatchAsync = { Ok: (value: T) => Promise; Err: (error: E) => Promise; }; export declare class ResultImpl { #private; constructor(k: boolean, v: T | E); [Symbol.iterator](): Iterator, T, any>; private unwrapFailed; /** * Matches the result with two functions. * * **Examples** * * ``` * const x: Result = Ok(2) * assert.strictEqual(x.match({ * Ok: (v) => v * 2, * Err: (e) => e.length, * }), 4) * * const y: Result = Err("error") * assert.strictEqual(y.match({ * Ok: (v) => v * 2, * Err: (e) => e.length, * }), 5) * ``` */ match(pattern: ResultMatch): A | B; matchAsync(pattern: ResultMatchAsync): Promise; /** * Returns the contained value, if it exists. */ value(): T | undefined; /** * Returns the contained error, if it exists. */ error(): E | undefined; /** * Returns `true` if the result is `Ok`. */ isOk(): this is Ok; /** * Returns `true` if the result is `Ok` and the value satisfies the predicate. * * Maybe not as useful as using `result.isOk() && f(result.value)`, because it doesn't narrow the type, but it's here for completeness. */ isOkAnd(f: (value: T) => boolean): this is Ok; /** * Returns `true` if the result is `Err`. */ isErr(): this is Err; /** * Returns `true` if the result is `Err` and the error satisfies the predicate. * * Maybe not as useful as using `result.isErr() && f(result.error)`, because it doesn't narrow the type, but it's here for completeness. */ isErrAnd(f: (error: E) => boolean): this is Err; /** * Converts from `Result` to `Option`. * * **Examples** * * ``` * const x: Result = Ok(2) * assert.strictDeepEqual(x.ok(), Some(2)) * * const y: Result = Err("Nothing here") * assert.strictDeepEqual(y.ok(), None) * ``` */ ok(): Option; /** * Converts from `Result` to `Option`. * * **Examples** * * ``` * const x: Result = Ok(2) * assert.strictDeepEqual(x.err(), None) * * const y: Result = Err("Nothing here") * assert.strictDeepEqual(y.err(), Some("Nothing here")) * ``` */ err(): Option; /** * Maps a `Result` to `Result` by applying a function to a contained `Ok` value, leaving an `Err` value untouched. * * **Examples** * * ``` * const x = Ok(10) * const mapped = x.map((n) => `number ${n}`) * assert.strictEqual(mapped.unwrap(), "number 10") * ``` */ map(f: (value: T) => U): Result; /** * Maps a `Result` to `AsyncResult` by applying an async function to a contained `Ok` value, leaving an `Err` value untouched. * * **Examples** * * ``` * const x = Ok(10) * const mapped = x.mapAsync((n) => Promise.resolve(`number ${n}`)) * assert.strictEqual(await mapped.unwrap(), "number 10") * ``` */ mapAsync(f: (value: T) => Promise): AsyncResult; /** * Returns the provided default (if `Err`), or applies a function to the contained value (if `Ok`). * * **Examples** * * ``` * const x: Result = Ok("foo"); * assert.strictEqual(x.mapOr(42, (v) => v.length), 3); * * const y: Result = Err("bar"); * assert.strictEqual(y.mapOr(42, (v) => v.length), 42); * ``` */ mapOr(defaultValue: A, f: (value: T) => B): A | B; mapOrAsync(defaultValue: A, f: (value: T) => Promise): Promise; /** * Maps a `Result` to `A | B` by applying fallback function `defaultValue` to a contained `Err` value, or function `f` to a contained `Ok` value. * * **Examples** * * ``` * const k = 21 * * let x: Result = Ok("foo") * assert.strictEqual(x.mapOrElse(() => k * 2, (v) => v.length), 3) * * x = Err("bar") * assert.strictEqual(x.mapOrElse(() => k * 2, (v) => v.length), 42) * ``` */ mapOrElse(defaultValue: (error: E) => A, f: (value: T) => B): A | B; mapOrElseAsync(defaultValue: (error: E) => Promise, f: (value: T) => Promise): Promise; /** * Maps a `Result` to `Result` by applying a function to a contained `Err` value, leaving an `Ok` value untouched. * * **Examples** * * ``` * const x = Err("error") * const mapped = x.mapErr((s) => s.length) * assert.strictEqual(mapped.unwrapErr(), 5) * ``` */ mapErr(f: (error: E) => F): Result; /** * Maps a `Result` to `AsyncResult` by applying an async function to a contained `Err` value, leaving an `Ok` value untouched. * * **Examples** * * ``` * const x = Err("error") * const mapped = x.mapErrAsync((s) => Promise.resolve(s.length)) * assert.strictEqual(await mapped.unwrapErr(), 5) * ``` */ mapErrAsync(f: (error: E) => Promise): AsyncResult; /** * Calls the provided function with the contained value (if `Ok`). * * **Examples** * * ``` * const x = Ok(2) * x.inspect((v) => console.log(v)) * ``` */ inspect(f: (value: T) => void): this; /** * Calls the provided async function with the contained value (if `Ok`). * * **Examples** * * ``` * const x = Ok(2) * x.inspectAsync((v) => Promise.resolve(console.log(v))) * ``` */ inspectAsync(f: (value: T) => Promise): AsyncResult; /** * Calls the provided function with the contained error (if `Err`). * * **Examples** * * ``` * const x = Err("error") * x.inspectErr((e) => console.error(e)) * ``` */ inspectErr(f: (error: E) => void): this; /** * Calls the provided async function with the contained error (if `Err`). * * **Examples** * * ``` * const x = Err("error") * x.inspectErrAsync((e) => Promise.resolve(console.error(e))) * ``` */ inspectErrAsync(f: (error: E) => Promise): AsyncResult; /** * Returns the contained `Ok` value. * * Throws `Panic` if the value is an `Err`, with a message containing `message` and content of the `Err` as `cause`. * * **Examples** * * ``` * const x = Err("emergency failure") * x.expect("Testing expect") // throws Panic: Testing expect * ``` */ expect(message: string): T; /** * Returns the contained `Ok` value. * * Throws `Panic` if the value is an `Err`, with a message containing the content of the `Err` and `this` as `cause`. * * **Examples** * * ``` * const x = Err("emergency failure") * x.unwrap() // throws Panic: called * ``` */ unwrap(): T; /** * Returns the contained `Err` value. * * Throws `Panic` if the value is an `Ok`, with a message containing `message` and content of the `Ok` as `cause`. * * **Examples** * * ``` * const x = Ok(2) * x.expectErr("Testing expectErr") // throws Panic: Testing expectErr * ``` */ expectErr(message: string): E; /** * Returns the contained `Err` value. * * Throws `Panic` if the value is an `Ok`, with a message containing the content of the `Ok` and `this` as `cause`. * * **Examples** * * ``` * const x = Ok(2) * x.unwrapErr() // throws Panic * ``` */ unwrapErr(): E; /** * Returns `other` if the result is `Ok`, otherwise returns `this` (as `Err`). * * **Examples** * * ``` * let x: Result = Ok(2) * let y: Result = Err("late error") * assert.deepStrictEqual(x.and(y), Err("late error")) * * x = Err("early error") * y = Ok("foo") * assert.deepStrictEqual(x.and(y), Err("early error")) * * x = Err("not a 2") * y = Err("late error") * assert.deepStrictEqual(x.and(y), Err("not a 2")) * * x = Ok(2) * y = Ok("different result type") * assert.deepStrictEqual(x.and(y), Ok("different result type")) * ``` */ and(other: Result): Result; /** * Calls `f` if the result is `Ok`, otherwise returns `this` (as `Err`). * * **Examples** * * ``` * let x: Result = Ok(2) * assert.deepStrictEqual(x.andThen((n) => Ok(n * 2)), Ok(4)) * * let y: Result = Err("late error") * assert.deepStrictEqual(y.andThen((n) => Ok(n * 2)), Err("late error")) * ``` */ andThen(f: (value: T) => Result): Result; /** * Calls `f` if the result is `Ok`, otherwise returns `this` (as `Err`). * * **Examples** * * ``` * let x: Result = Ok(2) * assert.deepStrictEqual(x.andThenAsync((n) => Promise.resolve(Ok(n * 2))), Ok(4)) * * let y: Result = Err("late error") * assert.deepStrictEqual(y.andThenAsync((n) => Promise.resolve(Ok(n * 2))), Err("late error")) * ``` */ andThenAsync(f: (value: T) => AsyncResult | Promise>): AsyncResult; /** * Returns `other` if the result is `Err`, otherwise returns `this` (as `Ok`). * * **Examples** * * ``` * let x: Result = Ok(2) * let y: Result = Err("late error") * assert.deepStrictEqual(x.or(y), Ok(2)) * assert.deepStrictEqual(y.or(x), Ok(2)) * ``` */ or(other: Result): Result; /** * Calls `f` if the result is `Err`, otherwise returns `this` (as `Ok`). * * **Examples** * * ``` * let x: Result = Ok(2) * assert.deepStrictEqual(x.orElse((e) => Err(e + "bar")), Ok(2)) * * let y: Result = Err("foo") * assert.deepStrictEqual(y.orElse((e) => Err(e + "bar")), Err("foobar")) * ``` */ orElse(f: (error: E) => Result): Result; /** * Calls `f` if the result is `Err`, otherwise returns `this` (as `Ok`). * * **Examples** * * ``` * let x: Result = Ok(2) * assert.deepStrictEqual(x.orElseAsync((e) => Promise.resolve(Err(e + "bar"))), Ok(2)) * * let y: Result = Err("foo") * assert.deepStrictEqual(y.orElseAsync((e) => Promise.resolve(Err(e + "bar"))), Err("foobar")) * ``` */ orElseAsync(f: (error: E) => AsyncResult | Promise>): AsyncResult; /** * Returns the contained `Ok` value or a provided default. * * **Examples** * * ``` * const x: Result = Ok(2) * assert.strictEqual(x.unwrapOr(0), 2) * * const y: Result = Err("error") * assert.strictEqual(y.unwrapOr(0), 0) * ``` */ unwrapOr(defaultValue: U): T | U; /** * Returns the contained `Ok` value or computes it from `defaultValue`. * * **Examples** * * ``` * const x: Result = Ok(2) * assert.strictEqual(x.unwrapOrElse(() => 0), 2) * * const y: Result = Err("error") * assert.strictEqual(y.unwrapOrElse(() => 0), 0) * ``` */ unwrapOrElse(defaultValue: (error: E) => U): T | U; unwrapOrAsync(defaultValue: (error: E) => Promise): Promise; /** * Converts from `Result, E>` to `Result`. * * **Examples** * * ``` * const x: Result, string> = Ok(Ok(2)) * assert.deepStrictEqual(x.flatten(), Ok(2)) * * const y: Result, string> = Ok(Err("late error")) * assert.deepStrictEqual(y.flatten(), Err("late error")) * * const z: Result, string> = Err("early error") * assert.deepStrictEqual(z.flatten(), Err("early error")) * ``` */ flatten(this: Result, E>): Result; toObject(): { isOk: true; value: T; } | { isOk: false; error: E; }; toJSON(): { meta: "Ok"; value: T; } | { meta: "Err"; error: E; }; toString(): `Ok(${string})` | `Err(${string})`; [symbols.inspect](): ReturnType["toString"]>; } export interface Ok extends ResultImpl { [symbols.tag]: "Ok"; value(): T; error(): undefined; unwrap(): T; unwrapErr(): never; expect(message: string): T; expectErr(message: string): never; } /** * Contains the success value. */ export declare function Ok(): Ok; export declare function Ok(value: T): Ok; export interface Err extends ResultImpl { [symbols.tag]: "Err"; value(): undefined; error(): E; unwrap(): never; unwrapErr(): E; expect(message: string): never; expectErr(message: string): E; } /** * Contains the error value. */ export declare function Err(): Err; export declare function Err(error: E): Err; /** * `Result` is a type that represents either success (`Ok`) or failure (`Err`). * * `Result` is the type used for returning errors. It is a discriminated union with the variants, `Ok`, representing success and containing a value, and `Err`, representing error and containing an error value. * * Functions return `Result` whenever errors are expected and recoverable. */ export type Result = Ok | Err; export declare function Result(): void; export declare namespace Result { var from: (f: () => T) => Result; var fromPromise: (promise: Promise) => AsyncResult; }