//#region src/option.d.ts /** * Present value variant. * * @template A Value type. * * @example * const option = new Some(42); * option.value // 42 * option.status // "some" */ declare class Some { readonly value: A; readonly status: "some"; constructor(value: A); /** Returns true, narrowing Option to Some. */ isSome(): this is Some; /** Returns false, narrowing Option to None. */ isNone(): this is None; /** * Transforms value. * * @template B Transformed type. * @param fn Transformation function. * @returns Some with transformed value. * @throws {Panic} If fn throws. * * @example * some(2).map(x => x * 2) // Some(4) */ map(fn: (a: A) => B): Some; /** * Chains Option-returning function. * * @template B New value type. * @param fn Function returning Option. * @returns Option from fn. * @throws {Panic} If fn throws. * * @example * some(2).andThen(x => x > 0 ? some(x) : none()) // Some(2) */ andThen(fn: (a: A) => Option): Option; /** * Chains async Option-returning function. * * @template B New value type. * @param fn Async function returning Option. * @returns Promise of Option from fn. * @throws {Panic} If fn throws synchronously or rejects. * * @example * await some(1).andThenAsync(async x => some(await fetchData(x))) */ andThenAsync(fn: (a: A) => Promise>): Promise>; /** * Pattern matches on Option. * * @template T Return type. * @param handlers Some and none handlers. * @returns Result of some handler. * @throws {Panic} If handler throws. * * @example * some(2).match({ some: x => x * 2, none: () => 0 }) // 4 */ match(handlers: { some: (a: A) => T; none: () => T; }): T; /** * Extracts value. * * @param _message Ignored. * @returns The value. * * @example * some(42).unwrap() // 42 */ unwrap(_message?: string): A; /** * Returns value, ignoring fallback. * * @template B Fallback type. * @param _fallback Ignored. * @returns The value. * * @example * some(42).unwrapOr(0) // 42 */ unwrapOr(_fallback: B): A; /** * Returns value, ignoring fallback thunk. * * @template B Fallback type. * @param _fallback Ignored. * @returns The value. * * @example * some(42).unwrapOrElse(() => 0) // 42 */ unwrapOrElse(_fallback: () => B): A; /** * Runs side effect, returns self. * * @param fn Side effect function. * @returns Self. * @throws {Panic} If fn throws. * * @example * some(2).tap(console.log).map(x => x * 2) // logs 2, returns Some(4) */ tap(fn: (a: A) => void): Some; /** * Runs async side effect, returns self. * * @param fn Async side effect function. * @returns Promise of self. * @throws {Panic} If fn throws synchronously or rejects. * * @example * await some(2).tapAsync(async x => await log(x)) */ tapAsync(fn: (a: A) => Promise): Promise>; /** * Returns Some if predicate passes, None otherwise. * * @param predicate Test function. * @returns Some if predicate returns true, None otherwise. * @throws {Panic} If predicate throws. * * @example * some(5).filter(x => x > 3) // Some(5) * some(2).filter(x => x > 3) // None */ filter(predicate: (a: A) => boolean): Option; /** * Makes Some yieldable in Option.gen blocks. * Immediately returns the value without yielding. */ [Symbol.iterator](): Generator; } /** * Absent value variant (singleton). * * @example * const option = None.instance; * option.status // "none" */ declare class None { readonly status: "none"; private constructor(); /** Singleton instance. */ static readonly instance: None; /** Returns false, narrowing Option to Some. */ isSome(): this is Some; /** Returns true, narrowing Option to None. */ isNone(): this is None; /** * No-op on None, returns self. * * @template U New phantom value type. * @param _fn Ignored. * @returns Self. */ map(_fn: (a: never) => U): None; /** * No-op on None, returns self. * * @template U New phantom value type. * @param _fn Ignored. * @returns Self. */ andThen(_fn: (a: never) => Option): None; /** * No-op on None, returns Promise of self. * * @template U New phantom value type. * @param _fn Ignored. * @returns Promise of self. */ andThenAsync(_fn: (a: never) => Promise>): Promise; /** * Pattern matches on Option. * * @template R Return type. * @param handlers Some and none handlers. * @returns Result of none handler. * @throws {Panic} If handler throws. * * @example * none().match({ some: x => x, none: () => "empty" }) // "empty" */ match(handlers: { some: (a: never) => R; none: () => R; }): R; /** * Throws error with optional message. * * @param message Error message. * @throws Always throws. * * @example * none().unwrap() // throws Panic * none().unwrap("custom") // throws Panic("custom") */ unwrap(message?: string): never; /** * Returns fallback value. * * @template U Fallback type. * @param fallback Fallback value. * @returns Fallback. * * @example * none().unwrapOr(42) // 42 */ unwrapOr(fallback: U): U; /** * Computes and returns fallback value. * * @template U Fallback type. * @param fallback Fallback thunk. * @returns Result of fallback. * @throws {Panic} If fallback throws. * * @example * none().unwrapOrElse(() => 42) // 42 */ unwrapOrElse(fallback: () => U): U; /** * No-op on None, returns self. * * @param _fn Ignored. * @returns Self. */ tap(_fn: (a: never) => void): None; /** * No-op on None, returns Promise of self. * * @param _fn Ignored. * @returns Promise of self. */ tapAsync(_fn: (a: never) => Promise): Promise; /** * No-op on None, returns self. * * @param _predicate Ignored. * @returns Self. */ filter(_predicate: (a: never) => boolean): None; /** * Makes None yieldable in Option.gen blocks. * Yields None for proper generator short-circuiting. */ [Symbol.iterator](): Generator; } /** * Union type representing presence or absence of a value. * * @template A Value type. * * @example * type MaybeUser = Option; */ type Option = Some | None; /** * Infer the value type from an Option. * Distributive: InferValue | Some> = A | B */ type InferValue = O extends Some ? A : never; /** * Constraint for any Option type. * Used in Option.gen to accept flexible return types from generators. */ type AnyOption = Some | None; declare function some(): Some; declare function some(value: A): Some; declare function optionAwait(promise: Promise>): AsyncGenerator; /** Shape of a serialized Some over RPC. */ interface SerializedSome { status: "some"; value: A; } /** Shape of a serialized None over RPC. */ interface SerializedNone { status: "none"; } /** Shape of a serialized Option over RPC. */ type SerializedOption = SerializedSome | SerializedNone; /** * Utilities for creating and handling Option types. * * @example * const option = Option.from(maybeNull); * const value = option.map(x => x.id).unwrapOr("default"); */ declare const Option: { /** * Creates present value. * * @example * Option.some(42) // Some * Option.some() // Some */ readonly some: typeof some; /** * Creates absent value. * * @example * Option.none() // None */ readonly none: () => None; /** * Type guard for Some. * * @example * if (Option.isSome(option)) { option.value } */ readonly isSome: (option: Option) => option is Some; /** * Type guard for None. * * @example * if (Option.isNone(option)) { // handle absent } */ readonly isNone: (option: Option) => option is None; /** * Transforms value, passes None through. * * @example * Option.map(some(2), x => x * 2) // Some(4) * Option.map(x => x * 2)(some(2)) // Some(4) */ readonly map: { (option: Option, fn: (a: A) => B): Option; (fn: (a: A) => B): (option: Option) => Option; }; /** * Chains Option-returning function on Some. * * @example * Option.andThen(some(2), x => x > 0 ? some(x) : none()) // Some(2) */ readonly andThen: { (option: Option, fn: (a: A) => Option): Option; (fn: (a: A) => Option): (option: Option) => Option; }; /** * Chains async Option-returning function on Some. * * @example * await Option.andThenAsync(some(1), async x => some(await fetch(x))) */ readonly andThenAsync: { (option: Option, fn: (a: A) => Promise>): Promise>; (fn: (a: A) => Promise>): (option: Option) => Promise>; }; /** * Pattern matches on Option. * * @example * Option.match(some(2), { some: x => x * 2, none: () => 0 }) // 4 */ readonly match: { (option: Option, handlers: { some: (a: A) => T; none: () => T; }): T; (handlers: { some: (a: A) => T; none: () => T; }): (option: Option) => T; }; /** * Runs side effect on Some value, returns original option. * * @example * Option.tap(some(2), console.log) // logs 2, returns Some(2) */ readonly tap: { (option: Option, fn: (a: A) => void): Option; (fn: (a: A) => void): (option: Option) => Option; }; /** * Runs async side effect on Some value, returns original option. * * @example * await Option.tapAsync(some(2), async x => await log(x)) */ readonly tapAsync: { (option: Option, fn: (a: A) => Promise): Promise>; (fn: (a: A) => Promise): (option: Option) => Promise>; }; /** * Extracts value or throws. * * @example * Option.unwrap(some(42)) // 42 * Option.unwrap(none()) // throws Panic */ readonly unwrap: (option: Option, message?: string) => A; /** * Extracts value or returns fallback. * * @example * Option.unwrapOr(some(42), 0) // 42 * Option.unwrapOr(none(), 0) // 0 */ readonly unwrapOr: { (option: Option, fallback: B): A | B; (fallback: B): (option: Option) => A | B; }; /** * Generator-based composition for Option types. * * @example * const option = Option.gen(function* () { * const a = yield* getA(); // Option * const b = yield* getB(a); // Option * return Option.some({ a, b }); * }); * // Option<{a, b}> * * @example * // Async with Option.await * const option = await Option.gen(async function* () { * const a = yield* Option.await(fetchA()); * const b = yield* Option.await(fetchB(a)); * return Option.some({ a, b }); * }); */ readonly gen: { (body: () => Generator): Option>; (body: (this: This) => Generator, thisArg: This): Option>; (body: () => AsyncGenerator): Promise>>; (body: (this: This) => AsyncGenerator, thisArg: This): Promise>>; }; /** * Wraps Promise(option: Option) => SerializedOption; /** * Rehydrates serialized Option from RPC back into Some/None instances. * Returns null if not a serialized Option. * * @example * const option = Option.deserialize(rpcResponse); */ readonly deserialize: (value: unknown) => Option | null; /** * Splits array of Options into tuple of [someValues, noneCount]. * * @example * partition([some(1), none(), some(2)]) // [[1, 2], 1] */ readonly partition: (options: readonly Option[]) => [A[], number]; /** * Flattens nested Option into single Option. * * @example * const nested = Option.some(Option.some(42)); * Option.flatten(nested) // Some(42) */ readonly flatten: (option: Option>) => Option; /** * Converts nullable value to Option. * null and undefined become None, everything else becomes Some. * * @example * Option.from(user) // Some(user) if user exists * Option.from(null) // None * Option.from(undefined) // None */ readonly from: (value: A | null | undefined) => Option>; /** * Creates Option based on predicate result. * Some if predicate passes, None otherwise. * * @example * Option.fromPredicate(5, x => x > 3) // Some(5) * Option.fromPredicate(2, x => x > 3) // None * Option.fromPredicate(x => x > 3)(5) // Some(5) - pipeable */ readonly fromPredicate: { (value: A, predicate: (a: A) => a is B): Option; (value: A, predicate: (a: A) => boolean): Option; (predicate: (a: A) => a is B): (value: A) => Option; (predicate: (a: A) => boolean): (value: A) => Option; }; /** * Filters Option based on predicate. * Some becomes None if predicate fails. * * @example * Option.filter(some(5), x => x > 3) // Some(5) * Option.filter(some(2), x => x > 3) // None * Option.filter(x => x > 3)(some(5)) // Some(5) - pipeable */ readonly filter: { (option: Option, predicate: (a: A) => a is B): Option; (option: Option, predicate: (a: A) => boolean): Option; (predicate: (a: A) => a is B): (option: Option) => Option; (predicate: (a: A) => boolean): (option: Option) => Option; }; /** * Combines two Options into Option of tuple. * Returns Some([a, b]) if both are Some, None otherwise. * * @example * Option.zip(some(1), some("a")) // Some([1, "a"]) * Option.zip(some(1), none()) // None */ readonly zip: (optionA: Option, optionB: Option) => Option<[A, B]>; /** * Extracts value or computes fallback lazily. * * @example * Option.unwrapOrElse(some(42), () => 0) // 42 * Option.unwrapOrElse(none(), () => expensiveDefault()) // expensiveDefault() */ readonly unwrapOrElse: { (option: Option, fallback: () => B): A | B; (fallback: () => B): (option: Option) => A | B; }; }; //#endregion //#region src/error.d.ts /** * Unrecoverable error — user code threw inside Option operations. * * @example * // Panic in generator cleanup: * Option.gen(function* () { * try { * yield* Option.none(); * } finally { * throw new Error("cleanup failed"); // Panic! * } * }); * * // Panic in combinator: * Option.some(1).map(() => { throw new Error("oops"); }); // Panic! */ declare class Panic extends Error { readonly _tag: "Panic"; readonly cause?: unknown; constructor(args: { message: string; cause?: unknown; }); toJSON(): object; } /** * Type guard for Panic instances. * * @example * if (isPanic(value)) { value.cause } */ declare const isPanic: (value: unknown) => value is Panic; /** * Throw an unrecoverable Panic. * * @example * panic("something went wrong", cause); */ declare const panic: (message: string, cause?: unknown) => never; //#endregion export { type InferValue, None, Option, Panic, type SerializedNone, type SerializedOption, type SerializedSome, Some, isPanic, panic }; //# sourceMappingURL=index.d.mts.map