import { AsyncOption } from "./async_option"; import { AsyncResult } from "./async_result"; import { type Result } from "./result"; import * as symbols from "./symbols"; export type OptionMatch = { Some: (value: T) => A; None: () => B; }; export type OptionMatchAsync = { Some: (value: T) => Promise; None: () => Promise; }; export declare class OptionImpl { #private; constructor(some: boolean, x?: T); private unwrapFailed; /** * Matches the option to its content. * * **Examples** * * ``` * Some(42).match({ * Some: (value) => value + 1, * None: () => 0 * }) // => 43 * ``` */ match(pattern: OptionMatch): A | B; matchAsync(pattern: OptionMatchAsync): Promise; /** * Returns the contained `Some` value, if exists. */ value(): T | undefined; /** * Returns `true` if the option is a `Some` value. */ isSome(): this is Some; /** * Returns `true` if the option is a `Some` value and the contained value is equal to `value`. * * Maybe not as useful as using `option.isSome() && f(option.value)`, because it doesn't narrow the type, but it's here for completeness. */ isSomeAnd(predicate: (value: T) => boolean): this is Some; /** * Returns `true` if the option is a `None` value. */ isNone(): this is None; /** * Returns the contained `Some` value, if exists. * * Throws `Panic` with the provided message if the value is `None`. * * **Examples** * * ``` * Some(42).expect("The world is ending") // => 42 * None.expect("The world is ending") // => Panic: The world is ending * ``` */ expect(message: string): T; /** * Returns the contained `Some` value, if exists. * * Throws `Panic` with a default message if the value is `None`. * * **Examples** * * ``` * Some(42).unwrap() // => 42 * None.unwrap() // => Panic: called "unwrap()" on None * ``` */ unwrap(): T; /** * Returns the contained `Some` value, if exists. * * Otherwise, returns the provided default value. * * **Examples** * * ``` * Some(42).unwrapOr(0) // => 42 * None.unwrapOr(0) // => 0 * ``` */ unwrapOr(defaultValue: U): T | U; /** * Returns the contained `Some` value, if exists. * * Otherwise, computes the provided default value. * * **Examples** * * ``` * Some(42).unwrapOrElse(() => 0) // => 42 * None.unwrapOrElse(() => 0) // => 0 * ``` */ unwrapOrElse(defaultValue: () => U): T | U; unwrapOrElseAsync(defaultValue: () => Promise): Promise; /** * Maps an `Option` to `Option` by applying a function to a contained value. * * **Examples** * * ``` * Some(42).map(x => x + 1) // => Some(43) * None.map(x => x + 1) // => None * ``` */ map(f: (value: T) => U): Option; mapAsync(f: (value: T) => Promise): AsyncOption; /** * Calls `f` if the `Option` is `Some`, otherwise returns `None`. * * **Examples** * * ``` * Ok(42).inspect(console.log) // => 42 * ``` */ inspect(f: (value: T) => void): this; inspectAsync(f: (value: T) => Promise): AsyncOption; /** * Returns the provided default result (if none), or applies a function to the contained value (if any). * * **Examples** * * ``` * Some(42).mapOr(0, x => x + 1) // => 43 * None.mapOr(0, x => x + 1) // => 0 * ``` */ mapOr(defaultValue: A, f: (value: T) => B): A | B; mapOrAsync(defaultValue: A, f: (value: T) => Promise): Promise; /** * Returns the provided default result (if none), or computes a default value by applying a function to the contained value (if any). * * **Examples** * * ``` * const k = 21 * Some("foo").mapOrElse(() => k * 2, (v) => v.length) // => 3 * None.mapOrElse(() => k * 2, (v) => v.length) // => 42 * ``` */ mapOrElse(defaultValue: () => A, f: (value: T) => B): A | B; mapOrElseAsync(defaultValue: () => Promise, f: (value: T) => Promise): Promise; /** * Transforms the `Option` into a `Result`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`. * * **Examples** * * ``` * Some(42).okOr("failed") // => Ok(42) * None.okOr("failed") // => Err("failed") * ``` */ okOr(err: E): Result; /** * Transforms the `Option` into a `Result`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`. * * **Examples** * * ``` * Some(42).okOrElse(() => "failed") // => Ok(42) * None.okOrElse(() => "failed") // => Err("failed") * ``` */ okOrElse(err: () => E): Result; okOrElseAsync(err: () => Promise): AsyncResult; /** * Returns `None` if the option is `None`, otherwise returns `other`. * * **Examples** * * ``` * Some(42).and(Some(54)) // => Some(54) * Some(42).and(None) // => None * None.and(Some(54)) // => None * None.and(None) // => None * ``` */ and(other: Option): Option; /** * Returns `None` if the option is `None`, otherwise calls `f` with the wrapped value and returns the result. * * **Examples** * * ``` * Some(42).andThen(x => Some(x + 1)) // => Some(43) * Some(42).andThen(x => None) // => None * None.andThen(x => Some(x + 1)) // => None * ``` */ andThen(f: (value: T) => Option): Option; andThenAsync(f: (value: T) => Promise> | AsyncOption): AsyncOption; /** * Returns `None` if the option is `None`, otherwise calls `predicate` with the wrapped value and returns: * - `Some` if predicate returns true, and * - `None` if predicate returns false. * * **Examples** * * ``` * Some(42).filter(x => x > 40) // => Some(42) * Some(42).filter(x => x > 50) // => None * None.filter(x => x > 40) // => None * ``` */ filter(predicate: (value: T) => boolean): Option; /** * Returns the option if it contains a value, otherwise returns `other`. * * **Examples** * * ``` * Some(42).or(Some(54)) // => Some(42) * Some(42).or(None) // => Some(42) * None.or(Some(54)) // => Some(54) * None.or(None) // => None * ``` */ or(other: Option): Option; /** * Returns the option if it contains a value, otherwise calls `f` and returns the result. * * **Examples** * * ``` * Some(42).orElse(() => Some(54)) // => Some(42) * Some(42).orElse(() => None) // => Some(42) * None.orElse(() => Some(54)) // => Some(54) * None.orElse(() => None) // => None * ``` */ orElse(f: () => Option): Option; orElseAsync(f: () => Promise> | AsyncOption): AsyncOption; /** * Returns `Some` if exactly one of `this` and `other` is `Some`, otherwise returns `None`. * * **Examples** * * ``` * Some(42).xor(Some(54)) // => None * Some(42).xor(None) // => Some(42) * None.xor(Some(54)) // => Some(54) * None.xor(None) // => None * ``` */ xor(other: Option): Option; /** * Converts from `Option>` to `Option`. * * **Examples** * * ``` * Some(Some(42)).flatten() // => Some(42) * Some(None).flatten() // => None * ``` */ flatten(this: Option>): Option; toObject(): { isSome: true; value: T; } | { isSome: false; value: null; }; toJSON(): { meta: "Some"; value: T; } | { meta: "None"; value: null; }; toString(): `Some(${string})` | "None"; [symbols.inspect](): ReturnType["toString"]>; } export interface Some extends OptionImpl { [symbols.tag]: "Some"; value(): T; unwrap(): T; expect(message: string): T; } /** * Some value of type `T`. */ export declare function Some(value: T): Some; export interface None extends OptionImpl { [symbols.tag]: "None"; value(): undefined; unwrap(): never; expect(message: string): never; } /** * No value. */ export declare const None: None; /** * `Option` represents an optional value: every `Option` is either `Some` and contains a value, or `None`, and does not. */ export type Option = Some | None; export declare function Option(): void; export declare namespace Option { var from: (value: T | null | undefined) => Option; }