import { OptionClass } from "./option-impl";
import type { Result } from "./result";
/**
* @ignore
* @internal
*/
export type OptionSome = {
/** @ignore */
readonly result: true;
/**
* The contained value of type `A`, if {@link Option.isSome}() is true. If
* {@link Option.isNone}() is true, there's no value and the type is `never`.
*/
readonly value: A;
};
/**
* @ignore
* @internal
*/
export type OptionNone = {
readonly result: false;
readonly value: never;
};
/**
* A nullable value of type `A`.
*
* `Option` is essentially the same as `A | undefined`, but with a lot of
* useful methods attached, and with the ability to distinguish between a
* missing value and a present value that's `undefined`.
*
* You can construct an {@link Option} using either the {@link Some} function to
* wrap a present value of `A`, or the value {@link None} to signify a null
* value. You can also convert a value that's `A | undefined` into an
* {@link Option} by calling {@link Option.from}.
*
* @template A The type of the contained value.
*
* @interface
*/
export type Option = (OptionSome | OptionNone) & IOption;
/** @ignore */
export interface IOption {
/**
* Test if the {@link Option} contains a value.
*/
isSome(): this is OptionSome;
/**
* Test if the {@link Option} is empty.
*/
isNone(): this is OptionNone;
/**
* Assert that the {@link Option} contains a value.
*
* Throws a {@link TypeError} if it doesn't.
*/
assertSome(): asserts this is OptionSome;
/**
* Assert that the {@link Option} is empty.
*
* Throws a {@link TypeError} if it isn't.
*/
assertNone(): asserts this is OptionNone;
/**
* The `match` function takes two callbacks, one for each possible state of
* the {@link Option}, and calls the one that matches the actual state.
*/
match(onSome: (value: A) => B, onNone: () => B): B;
/**
* Call the provided function with the contained value if the {@link Option}
* isn't empty.
*/
ifSome(onSome: (value: A) => void): void;
/**
* Call the provided function if the {@link Option} is empty.
*/
ifNone(onNone: () => void): void;
/**
* If the {@link Option} isn't empty, call the provided function with the
* contained value and return the result of the function, which must be
* another {@link Option}. If the {@link Option} is empty, return
* {@link None} without calling the function.
*
* This is the monadic bind function, for those who celebrate.
*/
chain(f: (value: A) => Option): Option;
/**
* If the {@link Option} is empty, call the provided function and return its
* result, which must be another {@link Option} of `A`. If the {@link Option} is
* {@link Some}, return the {@link Some} value without calling the function.
*/
chainNone(f: () => Option): Option;
/**
* If the {@link Option} isn't empty, transform its contained value using
* the provided function. Otherwise, {@link None} is returned.
*/
map(f: (value: A) => B): Option;
/**
* Given another {@link Option} containing a function from `A` to `B`, if
* both {@link Option}s are {@link Some}, call that function with the value
* of this {@link Option} and return an `Option` containing the
* function's return value. If either {@link Option} is {@link None}, return
* {@link None}.
*/
ap(f: Option<(a: A) => B>): Option;
/**
* If the {@link Option} isn't empty, return the provided {@link Option} of `B`, otherwise return {@link None}.
*/
and(option: Option): Option;
/**
* If the {@link Option} is empty, return the provided {@link Option}, otherwise return the original {@link Option}.
*/
or(option: Option): Option;
/**
* Return the value contained in the {@link Option} if it's not empty, or return `defaultValue` otherwise.
*/
getOr(defaultValue: A): A;
/**
* Return the value contained in the {@link Option} if it's not empty, or call the provided function and return its result otherwise.
*/
getOrElse(f: () => A): A;
/**
* Convert the {@link Option} into a {@link Result}, using the provided `error` value if the {@link Option} is empty.
*/
okOr(error: E): Result;
/**
* Convert the {@link Option} into a {@link Result}, calling the provided function to obtain an error value if the {@link Option} is empty.
*/
okOrElse(f: () => E): Result;
/**
* Convert the {@link Option} into an optional value of `A`.
*
* If the {@link Option} is {@link None}, this method returns `undefined`.
* If the {@link Option} is {@link Some}, it returns a value of `A`. Note
* that this value could also be `undefined`.
*/
unwrap(): A | undefined;
/**
* Convert the {@link Option} into a value of `A`, throwing a {@link TypeError}
* when empty, optionally with the provided error message.
*/
unwrapExact(message?: string): A;
/**
* Convert an {@link Option} into a JSON structure for serialisation.
*/
toJSON(): {
result: true;
value: A;
} | {
result: false;
};
}
/**
* Static methods on the {@link Option} object.
*/
export declare const Option: {
/**
* Create an {@link Option} from a value that's either `A` or `undefined`.
*
* If the provided value is `undefined`, it will create a {@link None}.
* Otherwise, it will create a {@link Some} containing the value.
*
* @see {@link Option.unwrap}
*
* @example
* // Guarded index lookup:
* function getIndex(array: A[], index: number): Option {
* return Option.from(array[index]);
* }
*/
from(value: A | undefined): Option;
/**
* Turn a function that returns `A | undefined` into a function that
* returns `Option`.
*
* 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 an `Option`
* instead of the expected `Option`.
*
* @example
* function nonNeg(n: number): number | undefined {
* return n >= 0 ? n : undefined;
* }
* const liftedNonNeg: (n: number) => Option = Option.lift(nonNeg);
*/
lift) => A | undefined>(fn: F): (...args: Parameters) => Option;
/**
* Create an {@link Option} from the output of {@link Option.toJSON}.
*/
fromJSON(doc: {
result: boolean;
value?: A;
}): Option;
/**
* Test whether an unknown value is an {@link Option}.
*
* @example
* function assertOption(value: unknown): void {
* if (Option.is(value)) {
* value.assertSome();
* }
* }
*/
is(value: unknown): value is Option;
/**
* The class constructor of {@link Option}s. You should never use this to
* construct {@link Option}s directly, preferring instead {@link Some} and
* {@link None}. It's exposed for use in `instanceof` checks, though
* calling {@link Option.is} to decide option-ness is preferred.
*
* @example
* import { expect } from "chai";
* expect(Some("pony")).to.be.an.instanceof(Option.Class);
*/
Class: typeof OptionClass;
};
/**
* Construct an {@link Option} containing the provided value.
*
* @example
* const option: Option = Some("Hello Joe");
*/
export declare function Some(value: A): Option;
/**
* An empty {@link Option}.
*
* @example
* const option: Option = None;
*/
export declare const None: Option;
/**
* Extract the value type from an {@link Option} type.
*
* @example
* type VoteOption = Option<"yes" | "no">;
* type VoteValue = SomeType; // "yes" | "no"
*/
export type SomeType = T extends Option ? A : never;