//#region src/option.d.ts /** * Represents some value of type `T`. */ type SomeOption = { readonly value: T; } & OptionMethods; /** * Represents the absence of a value of type `T`. */ type NoneOption = { readonly value: null; } & OptionMethods; /** * Type `Option` represents an optional value: every `Option` is either `Some` and contains a value, or `None`, and does not. * * `Option`s are commonly paired with pattern matching to query the presence of a value and take action, always accounting for the `None` case. * * @example * ```typescript * const x: Option = Some(5); * const y: Option = None(); * * if (x.isSome() && y.isSome()) console.log(x.value + y.value); * else console.log('One of the options is None'); * ``` * * @template T Contains the type of the value that may be present in the `Option`. */ type Option = SomeOption | NoneOption; interface OptionMethods { /** * Returns `true` if the option is a `Some` value. */ isSome(): this is SomeOption; /** * Returns `true` if the option is a `Some` and the value inside of it matches a predicate. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ isSomeAnd(f: (val: T) => boolean): boolean; /** * Returns `true` if the option is a `None` value. */ isNone(): this is NoneOption; /** * Returns `true` if the option is a `None` or the value inside of it matches a predicate. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ isNoneOr(f: (val: T) => boolean): boolean; /** * Returns the contained `Some` value. * * @throws Panics if the value is a `None` with a custom panic message provided by `msg`. * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ expect(msg: string): T; /** * Returns the contained `Some` value. * * @throws Panics if the self value equals `None`. */ unwrap(): T; /** * Returns the contained `Some` value or a provided default. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ unwrapOr(defaultVal: T): T; /** * Returns the contained `Some` value or computes it from a closure. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ unwrapOrElse(f: () => T): T; /** * Maps an `Option` to `Option` by applying a function to a contained value. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ map(f: (val: T) => U): Option; /** * Calls the provided closure with a reference to the contained value (if `Some`). * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ inspect(f: (val: T) => void): Option; /** * Returns the provided default result (if none), or applies a function to the contained value (if any). * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ mapOr(defaultVal: U, f: (val: T) => U): U; /** * Computes a default function result (if none), or applies a different function to the contained value (if any). * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ mapOrElse(defaultF: () => U, f: (val: T) => U): U; /** * Transforms the `Option` into a `Result`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ okOr(err: E): Result; /** * Transforms the `Option` into a `Result`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ okOrElse(errF: () => E): Result; /** * Returns an iterator over the possibly contained value. */ iter(): IterableIterator; /** * Returns `None` if the option is `None`, otherwise returns `optb`. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ and(optb: Option): Option; /** * Returns `None` if the option is `None`, otherwise calls `f` with the wrapped value and returns the result. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ andThen(f: (val: T) => Option): Option; /** * Returns `None` if the option is `None`, otherwise calls `predicate` with the wrapped value and returns: * - `Some(t)` if `predicate` returns `true` (where `t` is the wrapped value), and * - `None` if `predicate` returns `false`. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ filter(predicate: (val: T) => boolean): Option; /** * Returns the option if it contains a value, otherwise returns `optb`. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ or(optb: Option): Option; /** * Returns the option if it contains a value, otherwise calls `f` and returns the result. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ orElse(f: () => Option): Option; /** * Returns `Some` if exactly one of `this`, `optb` is `Some`, otherwise returns `None`. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ xor(optb: Option): Option; /** * Inserts `value` into the option, then returns a reference to it. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ insert(value: T): T; /** * Inserts `value` into the option if it is `None`, then returns a reference to the contained value. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ getOrInsert(value: T): T; /** * Inserts a value computed from `f` into the option if it is `None`, then returns a reference to the contained value. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ getOrInsertWith(f: () => T): T; /** * Takes the value out of the option, leaving a `None` in its place. */ take(): Option; /** * Takes the value out of the option, but only if the predicate evaluates to `true` on the value. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ takeIf(predicate: (val: T) => boolean): Option; /** * Replaces the actual value in the option by the value given in parameter, returning the old value if present, * leaving a `Some` in its place. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ replace(value: T): Option; /** * Converts from `Option>` to `Option`. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ flatten(this: Option>): Option; } /** * Some value of type `T`. * @param value The value to be wrapped in a `Some`. * @returns An `Option` representing the presence of a value. */ declare function Some(value: T): Option; /** * No value. * @returns An `Option` representing the absence of a value. */ declare function None(): Option; //#endregion //#region src/result.d.ts /** * The base interface for all errors returned by a `Result`. * It requires a `code` property which can be used to identify the error type. */ interface ResultError { code: string; } /** * Represents a successful `Result` containing a value of type `T`. */ type OkResult = { readonly value: T; readonly error: null; } & ResultMethods; /** * Represents a failed `Result` containing an error of type `E`. */ type ErrResult = { readonly value: null; readonly error: E; } & ResultMethods; /** * `Result` is the type used for returning and propagating errors. * * It is a type with the parameters, `Ok(T)`, representing success and containing a value, * and `Err(E)`, representing error and containing an error value. * * Functions return `Result` whenever errors are expected and recoverable. * * The error type `E` must extend `ResultError` which contains a `code` property of type `string`. * * @example * ```typescript * const divide = (a: number, b: number) => { * if (b === 0) return Err({ code: 'DIVIDE_BY_ZERO' }); * return Ok(a / b); * } * * const { value, error } = divide(10, 2); * if (error) console.error(error.code); * else console.log(value); * ``` * * @template T - Contains the success value. * @template E - Contains the error value. Must have a `code` property of type `string`. */ type Result = OkResult | ErrResult; interface ResultMethods { /** * Returns `true` if the result is `Ok`. */ isOk(): this is OkResult; /** * Returns `true` if the result is `Ok` and the value inside of it matches a predicate. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ isOkAnd(f: (val: T) => boolean): this is OkResult; /** * Returns `true` if the result is `Err`. */ isErr(): this is ErrResult; /** * Returns `true` if the result is `Err` and the value inside of it matches a predicate. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ isErrAnd(f: (err: E) => boolean): this is ErrResult; /** * Converts from `Result` to `Option`. * * Converts self into an `Option`, consuming self, and converting the error to `None`, if any. */ ok(): Option; /** * Converts from `Result` to `Option`. * * Converts self into an `Option`, consuming self, and discarding the success value, if any. */ err(): Option; /** * Maps a `Result` to `Result` by applying a function to a contained `Ok` value, leaving an `Err` value untouched. * * This function can be used to compose the results of two functions. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ map(f: (val: T) => U): Result; /** * Returns the provided default (if `Err`), or applies a function to the contained value (if `Ok`). * * Arguments passed to `mapOr` are eagerly evaluated; if you are passing the result of a function call, it is recommended to use `mapOrElse`, which is lazily evaluated. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ mapOr(fallback: U, f: (val: T) => U): U; /** * Maps a `Result` to `U` by applying fallback function `fallbackFn` to a contained `Err` value, or function `f` to a contained `Ok` value. * * This function can be used to unpack a successful result while handling an error. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ mapOrElse(fallbackFn: (err: E) => U, f: (val: T) => U): U; /** * Maps a `Result` to `Result` by applying a function to a contained `Err` value, leaving an `Ok` value untouched. * * This function can be used to pass through a successful result while handling an error. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ mapErr(f: (err: E) => F): Result; /** * Calls a function with a reference to the contained value if `Ok`. * * Returns the original result. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ inspect(f: (val: T) => void): Result; /** * Calls a function with a reference to the contained value if `Err`. * * Returns the original result. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ inspectErr(f: (err: E) => void): Result; /** * Returns an iterator over the possibly contained value. * * The iterator yields one value if the result is `Ok`, otherwise none. */ iter(): Iterable; /** * Returns the contained `Ok` value, consuming the `self` value. * * @throws Panics if the value is an `Err`, with a panic message including the passed message, and the content of the `Err`. * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ expect(msg: string): T; /** * Returns the contained `Ok` value, consuming the `self` value. * * @throws Panics if the value is an `Err`, with a panic message provided by the `Err`'s value. */ unwrap(): T; /** * Returns the contained `Err` value, consuming the `self` value. * * @throws Panics if the value is an `Ok`, with a panic message including the passed message, and the content of the `Ok`. * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ expectErr(msg: string): E; /** * Returns the contained `Err` value, consuming the `self` value. * * @throws Panics if the value is an `Ok`, with a custom panic message provided by the `Ok`'s value. */ unwrapErr(): E; /** * Returns `res` if the result is `Ok`, otherwise returns the `Err` value of `self`. * * Arguments passed to `and` are eagerly evaluated; if you are passing the result of a function call, it is recommended to use `andThen`, which is lazily evaluated. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ and(res: Result): Result; /** * Calls `f` if the result is `Ok`, otherwise returns the `Err` value of `self`. * * This function can be used for control flow based on `Result` values. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ andThen(f: (val: T) => Result): Result; /** * Returns `res` if the result is `Err`, otherwise returns the `Ok` value of `self`. * * Arguments passed to `or` are eagerly evaluated; if you are passing the result of a function call, it is recommended to use `orElse`, which is lazily evaluated. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ or(res: Result): Result; /** * Calls `f` if the result is `Err`, otherwise returns the `Ok` value of `self`. * * This function can be used for control flow based on result values. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ orElse(f: (err: E) => Result): Result; /** * Returns the contained `Ok` value or a provided default. * * Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing the result of a function call, it is recommended to use `unwrapOrElse`, which is lazily evaluated. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ unwrapOr(fallback: T2): T | T2; /** * Returns the contained `Ok` value or computes it from a closure. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ unwrapOrElse(f: (err: E) => T2): T | T2; /** * Converts from `Result, E>` to `Result`. * * @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code. */ flatten(this: Result, E>): Result; } /** * Contains the success value. * * @example * ```typescript * const { value } = Ok(42); * ``` * * @param value - The value to wrap in a successful result. * @returns A `Result` representing a successful outcome. */ declare function Ok(value: T): Result; /** * Contains the error value. * * @example * ```typescript * const { error } = Err({ code: 'NOT_FOUND' }); * ``` * * @param error - The error to wrap in a failed result. Must have a `code` property of type `string`. * @returns A `Result` representing a failed outcome. */ declare function Err(error: E): Result; //#endregion export { Err, ErrResult, None, NoneOption, Ok, OkResult, Option, Result, ResultError, Some, SomeOption };