/** * ***Brief*** * Converts `unknown` to `string`. * * ***Warning*** * Does not support circular references and circular objects will result in `[object Object]`. * * ***Example*** * ```ts * console.log(toString(42)); /// 42 * console.log(toString(true)); /// true * console.log(toString(null)); /// null * console.log(toString(undefined)); /// undefined * * console.log(toString("example")); /// example * * let object: { * color: string; * speed: { * min: number, * max: number * } * } = { * color: "Blue", * speed: { * min: 0, * max: 500 * } * }; * console.log(object); /// {"color":"Blue","speed":{"min":0,"max":500}} * * function foo(): void { * let x: string = 500; * return x; * } * console.log(foo); /// function foo() { * /// x = 500; * /// return x; * /// } * ``` */ declare function toString(unknown: unknown): string; /** * ***Brief*** * Creates a deep clone of the provided value using the structuredClone API. * * ***Example*** * ```ts * clone() * .resolve(e => { * if (e.code === "DOM.ERR_DATA_CLONE") { * /// ... * } * }) * .unlock(); * ``` */ declare function clone(value: T1): Result; type Result$0 = Result; type Fpv = { /** * ***Brief*** * Unwraps the wrapped value of type `T1`. * * ***Requirement*** * Does not `panic`. * * ***Example*** * ```ts * let unsafe: Unsafe = Unsafe(500n); * unsafe.unwrap(); * ``` */ unwrap(): bigint; decimals(): T1; representation(): bigint; eq(x: bigint): boolean; eq(x: Fpv): boolean; eq(x: Fpv.Compatible): boolean; lt(x: bigint): boolean; lt(x: Fpv): boolean; lt(x: Fpv.Compatible): boolean; gt(x: bigint): boolean; gt(x: Fpv): boolean; gt(x: Fpv.Compatible): boolean; lteq(x: bigint): boolean; lteq(x: Fpv): boolean; lteq(x: Fpv.Compatible): boolean; gteq(x: bigint): boolean; gteq(x: Fpv): boolean; gteq(x: Fpv.Compatible): boolean; add(x: bigint): Fpv; add(x: Fpv): Fpv; add(x: Fpv.Compatible): Fpv; sub(x: bigint): Fpv; sub(x: Fpv): Fpv; sub(x: Fpv.Compatible): Fpv; mul(x: bigint): Fpv; mul(x: Fpv): Fpv; mul(x: Fpv.Compatible): Fpv; div(x: bigint): Fpv.Result>; div(x: Fpv): Fpv.Result>; div(x: Fpv.Compatible): Fpv.Result>; pow(x: bigint): Fpv.Result>; pow(x: Fpv): Fpv.Result>; pow(x: Fpv.Compatible): Fpv.Result>; sqrt(): Fpv.Result>; convert(decimals: T2): Fpv.Result>; percentageOf(x: bigint): Fpv.Result>; percentageOf(x: Fpv): Fpv.Result>; percentageOf(x: Fpv.Compatible): Fpv.Result>; percentageGain(newValue: bigint): Fpv.Result>; percentageGain(newValue: Fpv): Fpv.Result>; percentageGain(newValue: Fpv.Compatible): Fpv.Result>; percentageLoss(newValue: bigint): Fpv.Result>; percentageLoss(newValue: Fpv): Fpv.Result>; percentageLoss(newValue: Fpv.Compatible): Fpv.Result>; percentageChange(newValue: bigint): Fpv.Result>; percentageChange(newValue: Fpv): Fpv.Result>; percentageChange(newValue: Fpv.Compatible): Fpv.Result>; sliceOf(percentage: bigint): Fpv.Result>; sliceOf(percentage: Fpv): Fpv.Result>; sliceOf(percentage: Fpv.Compatible): Fpv.Result>; addPercentage(percentage: bigint): Fpv.Result>; addPercentage(percentage: Fpv): Fpv.Result>; addPercentage(percentage: Fpv.Compatible): Fpv.Result>; subPercentage(percentage: bigint): Fpv.Result>; subPercentage(percentage: Fpv): Fpv.Result>; subPercentage(percentage: Fpv.Compatible): Fpv.Result>; toNumber(): number; }; /** * ***Warning*** * Does not support negative `decimals`. */ declare function Fpv(_v: Fpv.Compatible, _decimals: T1): Fpv.Result>; declare namespace Fpv { type Result = Result$0; type ErrorCode = "FPV.ERR_DIVISION_BY_ZERO" | "FPV.ERR_NEGATIVE_DECIMALS" | "FPV.ERR_NEGATIVE_EXPONENT" | "FPV.ERR_CANNOT_SQUARE_NAGATIVE"; type Compatible = Fpv | bigint; type Decimals = bigint; type Calculator = { unwrap(value: Compatible): bigint; eq(x: bigint, y: bigint): boolean; eq(x: Fpv, y: bigint): boolean; eq(x: bigint, y: Fpv): boolean; eq(x: Fpv, y: Fpv): boolean; eq(x: Compatible, y: Compatible): boolean; lt(x: bigint, y: bigint): boolean; lt(x: Fpv, y: bigint): boolean; lt(x: bigint, y: Fpv): boolean; lt(x: Fpv, y: Fpv): boolean; lt(x: Compatible, y: Compatible): boolean; gt(x: bigint, y: bigint): boolean; gt(x: Fpv, y: bigint): boolean; gt(x: bigint, y: Fpv): boolean; gt(x: Fpv, y: Fpv): boolean; gt(x: Compatible, y: Compatible): boolean; lteq(x: bigint, y: bigint): boolean; lteq(x: Fpv, y: bigint): boolean; lteq(x: bigint, y: Fpv): boolean; lteq(x: Fpv, y: Fpv): boolean; lteq(x: Compatible, y: Compatible): boolean; gteq(x: bigint, y: bigint): boolean; gteq(x: Fpv, y: bigint): boolean; gteq(x: bigint, y: Fpv): boolean; gteq(x: Fpv, y: Fpv): boolean; gteq(x: Compatible, y: Compatible): boolean; add(x: bigint, y: bigint, decimals: T1): Result>; add(x: Fpv, y: bigint, decimals: T1): Result>; add(x: bigint, y: Fpv, decimals: T1): Result>; add(x: Fpv, y: Fpv, decimals: T1): Result>; add(x: Compatible, y: Compatible, decimals: T1): Result>; sub(x: bigint, y: bigint, decimals: T1): Result>; sub(x: Fpv, y: bigint, decimals: T1): Result>; sub(x: bigint, y: Fpv, decimals: T1): Result>; sub(x: Fpv, y: Fpv, decimals: T1): Result>; sub(x: Compatible, y: Compatible, decimals: T1): Result>; mul(x: bigint, y: bigint, decimals: T1): Result>; mul(x: Fpv, y: bigint, decimals: T1): Result>; mul(x: bigint, y: Fpv, decimals: T1): Result>; mul(x: Fpv, y: Fpv, decimals: T1): Result>; mul(x: Compatible, y: Compatible, decimals: T1): Result>; div(x: bigint, y: bigint, decimals: T1): Result>; div(x: Fpv, y: bigint, decimals: T1): Result>; div(x: bigint, y: Fpv, decimals: T1): Result>; div(x: Fpv, y: Fpv, decimals: T1): Result>; div(x: Compatible, y: Compatible, decimals: T1): Result>; pow(x: bigint, y: bigint, decimals: T1): Result>; pow(x: Fpv, y: bigint, decimals: T1): Result>; pow(x: bigint, y: Fpv, decimals: T1): Result>; pow(x: Fpv, y: Fpv, decimals: T1): Result>; pow(x: Compatible, y: Compatible, decimals: T1): Result>; sqrt(x: bigint, decimals: T1): Result>; sqrt(x: Fpv, decimals: T1): Result>; sqrt(x: Compatible, decimals: T1): Result>; convert(x: bigint, oldDecimals: T1, newDecimals: T2): Result>; convert(x: Fpv, oldDecimals: T1, newDecimals: T2): Result>; convert(x: Compatible, oldDecimals: T1, newDecimals: T2): Result>; percentageOf(x: bigint, y: bigint, decimals: T1): Result>; percentageOf(x: Fpv, y: bigint, decimals: T1): Result>; percentageOf(x: bigint, y: Fpv, decimals: T1): Result>; percentageOf(x: Fpv, y: Fpv, decimals: T1): Result>; percentageOf(x: Compatible, y: Compatible, decimals: T1): Result>; percentageGain(oldValue: bigint, newValue: bigint, decimals: T1): Result>; percentageGain(oldValue: Fpv, newValue: bigint, decimals: T1): Result>; percentageGain(oldValue: bigint, newValue: Fpv, decimals: T1): Result>; percentageGain(oldValue: Fpv, newValue: Fpv, decimals: T1): Result>; percentageGain(oldValue: Compatible, newValue: Compatible, decimals: T1): Result>; percentageLoss(oldValue: bigint, newValue: bigint, decimals: T1): Result>; percentageLoss(oldValue: Fpv, newValue: bigint, decimals: T1): Result>; percentageLoss(oldValue: bigint, newValue: Fpv, decimals: T1): Result>; percentageLoss(oldValue: Fpv, newValue: Fpv, decimals: T1): Result>; percentageLoss(oldValue: Compatible, newValue: Compatible, decimals: T1): Result>; percentageChange(oldValue: bigint, newValue: bigint, decimals: T1): Result>; percentageChange(oldValue: Fpv, newValue: bigint, decimals: T1): Result>; percentageChange(oldValue: bigint, newValue: Fpv, decimals: T1): Result>; percentageChange(oldValue: Fpv, newValue: Fpv, decimals: T1): Result>; percentageChange(oldValue: Compatible, newValue: Compatible, decimals: T1): Result>; sliceOf(x: bigint, percentage: bigint, decimals: T1): Result>; sliceOf(x: Fpv, percentage: bigint, decimals: T1): Result>; sliceOf(x: bigint, percentage: Fpv, decimals: T1): Result>; sliceOf(x: Fpv, percentage: Fpv, decimals: T1): Result>; sliceOf(x: Compatible, percentage: Compatible, decimals: T1): Result>; addPercentage(x: bigint, percentage: bigint, decimals: T1): Result>; addPercentage(x: Fpv, percentage: bigint, decimals: T1): Result>; addPercentage(x: bigint, percentage: Fpv, decimals: T1): Result>; addPercentage(x: Fpv, percentage: Fpv, decimals: T1): Result>; addPercentage(x: Compatible, percentage: Compatible, decimals: T1): Result>; subPercentage(x: bigint, percentage: bigint, decimals: T1): Result>; subPercentage(x: Fpv, percentage: bigint, decimals: T1): Result>; subPercentage(x: bigint, percentage: Fpv, decimals: T1): Result>; subPercentage(x: Fpv, percentage: Fpv, decimals: T1): Result>; subPercentage(x: Compatible, percentage: Compatible, decimals: T1): Result>; }; const Calculator: Calculator; } type Array$0$1 = Array; /** * ***Brief*** * A type that represents an optional value, encapsulating either a value `Some` * or the absence of a value `None`. */ type Option = Some | None; declare namespace Option { type Async = Promise>; type Array = Array$0$1>; /** * ***Brief*** * Utility class for handling tasks within `Option`. */ type Handler = { /** * ***Brief*** * Wraps a value into an `Option`, turning `null` or `undefined` to `None`. * * ***Example*** * ```ts * let foo: string | undefined; * Option * .flag(foo) * .map(foo => { * /// ... * }); * ``` */ flag(value: T1 | null | undefined): Option; /** * ***Brief*** * Iterates through an array of `Option`, short-circuiting at the first `None`. * * ***Example*** * ```ts * let o0: Option<200n>; * let o1: Option<201n>; * let o2: Option<202n>; * let o: Option<[200n, 201n, 202n]> = Option.all([o0, o1, o2]); * ``` */ all>(...options: T1): Option>; /** * ***Brief*** * Iterates through an array of `Option`, short-circuiting at the first `Some`. * * ***Example*** * ```ts * let o0: Option<200n>; * let o1: Option<201n>; * let o2: Option<202n>; * let o: Option<200n | 201n | 202n> = Option.any([o0, o1, o2]); * ``` */ any>(...options: T1): Option[number]>; }; const Handler: Handler; } type Error = BrandedStruct<"Error"> & { /** * ***Brief*** * The unique identifier for this error. */ code: T1; /** * ***Brief*** * A human-readable message that explains the nature of the error. */ message: Option; /** * ***Brief*** * An optional value that can hold additional data or context related to the error. */ payload: Option; /** * ***Brief*** * The stack trace associated with the error. */ stack: string; }; /** * ***Brief*** * A custom error with an optional message and payload for strongly-typed errors. * * ***Note*** * This is a general-purpose error structure to manage domain-specific error codes and provide better context. */ declare function Error(_configuration: Error.Configuration): Error; declare function Error(_code: T1, _message?: string, _payload?: T2): Error; declare namespace Error { type Configuration = { code: T1; message?: string; payload?: T2; stack?: string; handler?: Error.Handler; }; type Task = Closure<[e: Error], void>; type Handler = { match(unknown: unknown): unknown is Error; match(unknown: unknown, task: Task): unknown is Error; match(unknown: unknown, code: T1): unknown is Error; match(unknown: unknown, code: T1, task: Task): unknown is Error; /** * ***Brief*** * `panic` throws an error with optional message and stack trace location. * * ***Example*** * ```ts * panic(Error("MATH.ERR_DIVISION_BY_ZERO")); * panic(Error({ * code: "MATH.ERR_DIVISION_BY_ZERO", * message: Some("Math: Cannot divide by zero."), * payload: None, * stack: StackTrace(...) * })); * panic("An unrecoverable error has occured."); * ``` */ panic(e: Error, handler?: Handler): never; panic(code: T1, at?: Function, handler?: Handler): never; localStackTrace(at: Function): Option; }; const Handler: Handler; } type Unsafe = Parsable & { inspect(): unknown; }; declare function Unsafe(_value: unknown): Unsafe; declare const flag: typeof Option.Handler.flag; declare const allO: typeof Option.Handler.all; declare const anyO: typeof Option.Handler.any; declare const allR: typeof Result.Handler.all; declare const anyR: typeof Result.Handler.any; declare const wrap: typeof Result.Handler.wrap; declare const wrapAsync: typeof Result.Handler.wrapAsync; type Some = Branded<"Some"> & { /** * ***Brief*** * `some` checks if the current instance is `Some`. * * ***Example*** * ```ts * let option: Option<200n>; * if (option.some()) { * let value: 200n = option.unlock(); * /// ... * } * ``` */ some(): this is Some; /** * ***Brief*** * `none` checks if the current instance is `None`. * * ***Example*** * ```ts * let option: Option<200n>; * if (option.none()) { * /// `Option` cannot `unlock` because it is `None`. * /// ... * } * ``` */ none(): this is None; /** * ***Brief*** * `expect` terminates with `panic` if the `Option` is `None`. * * ***Warning*** * Reserved for debugging or unrecoverable errors. * * ***Example*** * ```ts * let option: Option<200n>; * let status: 200n = option.expect("This is unexpected and unrecoverable."); * ``` */ expect(): T1; expect(__: unknown): T1; expect(__?: unknown): T1; /** * ***Brief*** * Unwraps the wrapped value of type `T1`. * * ***Requirement*** * Does not `panic`. * * ***Example*** * ```ts * let unsafe: Unsafe = Unsafe(500n); * unsafe.unwrap(); * ``` */ unwrap(): T1; /** * ***Brief*** * Retrieves the value of a `Some`, or falls back to the provided value if it’s `None`. * * ***Example*** * ```ts * let option: Option<200n> = None; * let status: 200n = option.unwrapOr(200n); * console.log(status); /// 200n. * ``` */ unwrapOr(__: unknown): T1; /** * ***Brief*** * Chains an task until the first `None` is encountered. * * ***Example*** * ```ts * let option: Option<200n> = None; * option * .and(value => { * /// Task is skipped because `Option` is `None`. * /// ... * return Some(value + 1n); * }) * .and(value => { * /// Task is skipped because `Option` is `None`. * /// ... * }); * ``` */ and(task: Closure<[T1], Option>): Option; /** * ***Brief*** * Transforms the `Some` value if present, but if this is already an `None`, it remains unchanged. * * ***Example*** * ```ts * let option0: Option<200n> = Some(200n); * let option1: Option<201n> = option.map(value => { * /// Task is run because `Option` is `Some`. * /// ... * return value + 1n; * }); * ``` */ map(task: Closure<[T1], T2>): Some; /** * ***Brief*** * Converts an `Option` to a `Result`. * * ***Example*** * ```ts * let option: Option<200n>; * let result: Result<200n, 404n> = option.toResult(404n); * ``` */ toResult(__: unknown): Ok; }; /** * ***Brief*** * The value within an `Option`. */ declare function Some(_value: T1): Some; declare namespace Some { type From> = T1 extends Some ? Some : never; type FromAll>> = { [T2 in keyof T1]: T1[T2] extends Some ? From : never; }; type ValFrom> = T1 extends Some ? T2 : never; type ValFromAll>> = { [T2 in keyof T1]: ValFrom; }; } type DomError = Error; declare function DomError(): DomError; declare function DomError(_legacy: DOMException): DomError; declare namespace DomError { type Code = "DOM.ERR_INDEX_SIZE" | "DOM.ERR_HIERARCHY_REQUEST" | "DOM.ERR_WRONG_DOCUMENT" | "DOM.ERR_INVALID_CHARACTER" | "DOM.ERR_NO_MODIFICATION_ALLOWED" | "DOM.ERR_NOT_FOUND" | "DOM.ERR_NOT_SUPPORTED" | "DOM.ERR_INVALID_STATE" | "DOM.ERR_ATTRIBUTE_IN_USE" | "DOM.ERR_SYNTAX" | "DOM.ERR_INVALID_MODIFICATION" | "DOM.ERR_NAMESPACE" | "DOM.ERR_INVALID_ACCESS" | "DOM.ERR_TYPE_MISMATCH" | "DOM.ERR_SECURITY" | "DOM.ERR_NETWORK" | "DOM.ERR_ABORT" | "DOM.ERR_URL_MISMATCH" | "DOM.ERR_QUOTA_EXCEEDED" | "DOM.ERR_TIMEOUT" | "DOM.ERR_INVALID_NODE_TYPE" | "DOM.ERR_DATA_CLONE" | "DOM.ERR_ENCODING" | "DOM.ERR_NOT_READABLE" | "DOM.ERR_UNKNOWN" | "DOM.ERR_CONSTRAINT" | "DOM.ERR_DATA" | "DOM.ERR_TRANSACTION_INACTIVE" | "DOM.ERR_READ_ONLY" | "DOM.ERR_VERSION" | "DOM.ERR_OPERATION" | "DOM.ERR_NOT_ALLOWED"; namespace Exception { const NameRemap: Record; const CodeRemap: Record; type Name = "EncodingError" | "NotReadableError" | "UnknownError" | "ConstraintError" | "DataError" | "TransactionInactiveError" | "ReadOnlyError" | "VersionError" | "OperationError" | "NotAllowedError"; type Code = 1 | 3 | 4 | 5 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25; } } type None = Branded<"None"> & { /** * ***Brief*** * `some` checks if the current instance is `Some`. * * ***Example*** * ```ts * let option: Option<200n>; * if (option.some()) { * let value: 200n = option.unlock(); * /// ... * } * ``` */ some(): this is Some; /** * ***Brief*** * `none` checks if the current instance is `None`. * * ***Example*** * ```ts * let option: Option<200n>; * if (option.none()) { * /// `Option` cannot `unlock` because it is `None`. * /// ... * } * ``` */ none(): this is None; /** * ***Brief*** * `expect` terminates with `panic` if the `Option` is `None`. * * ***Warning*** * Reserved for debugging or unrecoverable errors. * * ***Example*** * ```ts * let option: Option<200n>; * let status: 200n = option.expect("This is unexpected and unrecoverable."); * ``` */ expect(): never; expect(message: string): never; expect(message?: string): never; /** * ***Brief*** * Retrieves the value of a `Some`, or falls back to the provided value if it’s `None`. * * ***Example*** * ```ts * let option: Option<200n> = None; * let status: 200n = option.unwrapOr(200n); * console.log(status); /// 200n. * ``` */ unwrapOr(fallback: T2): T2; /** * ***Brief*** * Chains an task until the first `None` is encountered. * * ***Example*** * ```ts * let option: Option<200n> = None; * option * .and(value => { * /// Task is skipped because `Option` is `None`. * /// ... * return Some(value + 1n); * }) * .and(value => { * /// Task is skipped because `Option` is `None`. * /// ... * }); * ``` */ and(__: unknown): None; /** * ***Brief*** * Transforms the `Some` value if present, but if this is already an `None`, it remains unchanged. * * ***Example*** * ```ts * let option0: Option<200n> = Some(200n); * let option1: Option<201n> = option.map(value => { * /// Task is run because `Option` is `Some`. * /// ... * return value + 1n; * }); * ``` */ map(__: unknown): None; /** * ***Brief*** * Converts an `Option` to a `Result`. * * ***Example*** * ```ts * let option: Option<200n>; * let result: Result<200n, 404n> = option.toResult(404n); * ``` */ toResult(e: T1): Err; }; /** * ***Brief*** * The absence of a value or an "empty" state. * * ***Warning*** * Any operation attempting to access an `Option` must safely handle the `None` state or terminate with an error. */ declare const None: None; type Ok = { /** * ***Brief*** * `ok` checks if the current instance is `Ok`. * * ***Example*** * ```ts * let result: Result<200n, 404n>; * if (result.ok()) { * let value: 200n = result.unlock(); * /// ... * } * ``` */ ok(): this is Ok; /** * ***Brief*** * `err` checks if the current instance is `Err`. * * ***Example*** * ```ts * let result: Result<200n, 404n>; * if (result.err()) { * let e: 404n = result.inspect(); * /// ... * } * ``` */ err(): this is Err; /** * ***Brief*** * `expect` terminates with `panic` if the `Result` is `Err`. * * ***Warning*** * Reserved for debugging or unrecoverable errors. * * ***Example*** * ```ts * let result: Result<200n, 404n>; * let status: 200n = result.expect("This is unexpected and unrecoverable."); * ``` */ expect(): T1; expect(__: unknown): T1; expect(__?: unknown): T1; /** * ***Brief*** * Ensures that the `Result` is an `Err` and retrieves the error value inside. * * ***Warning*** * Reserved for debugging or unrecoverable errors. * * ***Example*** * ```ts * let result: Result<200n, 404n>; * let status: 404n = result.expectErr("This is unexpected and unrecoverable."); * ``` */ expectErr(): never; expectErr(message: string): never; expectErr(message?: string): never; /** * ***Brief*** * Unwraps the wrapped value of type `T1`. * * ***Requirement*** * Does not `panic`. * * ***Example*** * ```ts * let unsafe: Unsafe = Unsafe(500n); * unsafe.unwrap(); * ``` */ unwrap(): T1; /** * ***Brief*** * Retrieves the value of an `Ok`, or falls back to the provided value if it’s an `Err`. * * ***Example*** * ```ts * let result: Result<200n, 404n> = Err(404n); * let status: 200n = result.unwrapOr(200n); * console.log(status); /// 200n. * ``` */ unwrapOr(__: unknown): T1; /** * ***Brief*** * Recovers from the current error by applying a recovery function, transforming the `Err` into an `Ok`. * * ***Example*** * ```ts * let result: Result<200n, 404n> = Err(404n); * let status: 200n = result * .recover(() => { * return 200n; * }) * .unlock(); * ``` */ recover(__: unknown): Ok; /** * ***Brief*** * Applies a transformation to the `Ok` value and returns an `Err` instance with the transformed value. * * ***Example*** * ```ts * let result: Result<200n, 404n> = Ok(200n); * let e: 404n = result * .degrade(() => { * return 404n; * }) * .inspect(); * console.log(e); /// 404n. * ``` */ degrade(task: Function$1): Err; /** * ***Brief*** * Chains an task until the first `Err` is encountered. * * ***Example*** * ```ts * let result: Result<200n, 404n> = Err(404n); * result * .and(value => { * /// Task is skipped because `Result` is an `Err`. * /// ... * return Ok(value + 1n); * }) * .and(value => { * /// Task is skipped because `Result` is an `Err`. * /// ... * }); * ``` */ and(task: Closure<[T1], Ok>): Ok; and(task: Closure<[T1], Err>): Result; and(task: Closure<[T1], Result>): Result; /** * ***Brief*** * Transforms the `Ok` value if present, but if this is already an `Err`, it remains unchanged. * * ***Example*** * ```ts * let result0: Result<200n, 404n> = Ok(200n); * let result1: Result<201n, 404n> = result.map(value => { * /// Task is run because `Result` is `Ok`. * /// ... * return value + 1n; * }); * ``` */ map(task: Closure<[T1], T2>): Ok; /** * ***Brief*** * Transforms the error contained in the `Err` using the provided `task` function. * * ***Example*** * ```ts * let result: Result<200n, 404n> = Err(404n); * result * .mapErr(e => { * return e + 1n; * }) * .inspect(); /// 405n. * ``` * * ***Example*** * ```ts * let result: Result<200n, 404n> = Ok(200n); * result * .mapErr(e => { * /// Task is run because `Result` is `Ok`. * /// ... * }); * ``` */ mapErr(__: unknown): Ok; /** * ***Brief*** * Converts a `Result` to an `Option`. * * ***Example*** * ```ts * let result: Result<200n, 404n>; * let option: Option<200n> = result.toOption(); * ``` */ toOption(): Option; }; /** * ***Brief*** * The successful value within a `Result`. */ declare function Ok(_value: T1): Ok; declare namespace Ok { type From> = T1 extends Ok ? Ok : never; type FromAll>> = { [T2 in keyof T1]: T1[T2] extends Ok ? From : never; }; type ValFrom> = T1 extends Ok ? T2 : never; type ValFromAll>> = { [T2 in keyof T1]: ValFrom; }; } type Array$0 = Array; /** * ***Brief*** * A wrapper that encapsulates either a successful outcome `Ok` or a failure `Err`. * * ***Example*** * ```ts * function foo(): Result<200n, 404n> { * if () return Ok(200n); * return Err(404n); * } * * let result: Result<200n, 404n> = foo(); * if (result.ok()) { * let value: 200n = result.unlock(); * /// ... * } * ``` */ type Result = Ok | Err; declare namespace Result { type Async = Promise>; type Array = Array$0>; type Handler = { /** * ***Brief*** * Iterates through an array of `Result`, short-circuiting at the first `Err`. * * ***Example*** * ```ts * let r0: Result<200n, 404n>; * let r1: Result<201n, 405n>; * let r2: Result<202n, 406n>; * let r: Result<[200n, 201n, 202n], 404n | 405n | 406n> = Result.all([r0, r1, r2]); * ``` */ all>(results: T1): Result, Err.ValFromAll[number]>; /** * ***Brief*** * Iterates through an array of `Result`, short-circuiting at the first `Ok`. * * ***Example*** * ```ts * let r0: Result<200n, 404n>; * let r1: Result<201n, 405n>; * let r2: Result<202n, 406n>; * let r: Result<200n | 201n | 202n, [404n, 405n, 406n]> = Result.any([r0, r1, r2]); * ``` */ any>(results: T1): Result[number], Err.ValFromAll>; /** * ***Brief*** * Wraps an unsafe operation that might throw an error and returns a `Result`. * * ***Example*** * ```ts * await Result.wrap(() => { * /// Some unsafe task. * /// ... * return 200n; * }).mapErr(unsafe => { * unsafe * .parse((unknown): unknown is string => { * return typeof unknown is "string"; * }) * .map(string => { * /// ... * }); * return 200n; * }); * ``` */ wrap>(task: Closure, ...payload: T3): Result; /** * ***Brief*** * Wraps an asynchronous unsafe operation that may throw and returns a `Result`. * * * ***Example*** * ```ts * await Result.wrapAsync(async () => { * /// Some unsafe async task. * /// ... * return 200n; * }).mapErr(unsafe => { * unsafe * .parse((unknown): unknown is string => { * return typeof unknown is "string"; * }) * .map(string => { * /// ... * }); * return 200n; * }); * ``` */ wrapAsync>(task: AsyncClosure, ...payload: T3): Promise>; }; const Handler: Handler; } type Err = { /** * ***Brief*** * `ok` checks if the current instance is `Ok`. * * ***Example*** * ```ts * let result: Result<200n, 404n>; * if (result.ok()) { * let value: 200n = result.unlock(); * /// ... * } * ``` */ ok(): this is Ok; /** * ***Brief*** * `err` checks if the current instance is `Err`. * * ***Example*** * ```ts * let result: Result<200n, 404n>; * if (result.err()) { * let e: 404n = result.inspect(); * /// ... * } * ``` */ err(): this is Err; /** * ***Brief*** * Retrieves the error value encapsulated by the `Err` instance. * * ***Example*** * ```ts * let result: Result<200n, 404n>; * if (result.err()) { * let e: 404n = result.inspect(); * /// ... * } * ``` */ inspect(): T1; /** * ***Brief*** * Retrieves the stack trace of the error, generated at the time the `Err` instance was created. * * ***Example*** * ```ts * let result: Result<200n, 404n>; * if (result.err()) { * result * .stack() * .lines() * .forEach(line => { * let lineRepresentation: string = line.toString(); * /// ... * return; * }); * } * ``` */ stack(): string; /** * ***Brief*** * `expect` terminates with `panic` if the `Result` is `Err`. * * ***Warning*** * Reserved for debugging or unrecoverable errors. * * ***Example*** * ```ts * let result: Result<200n, 404n>; * let status: 200n = result.expect("This is unexpected and unrecoverable."); * ``` */ expect(): never; expect(message: string): never; expect(message?: string): never; /** * ***Brief*** * Ensures that the `Result` is an `Err` and retrieves the error value inside. * * ***Warning*** * Reserved for debugging or unrecoverable errors. * * ***Example*** * ```ts * let result: Result<200n, 404n>; * let status: 404n = result.expectErr("This is unexpected and unrecoverable."); * ``` */ expectErr(): T1; expectErr(__: unknown): T1; expectErr(__?: unknown): T1; /** * ***Brief*** * Retrieves the value of an `Ok`, or falls back to the provided value if it’s an `Err`. * * ***Example*** * ```ts * let result: Result<200n, 404n> = Err(404n); * let status: 200n = result.unwrapOr(200n); * console.log(status); /// 200n. * ``` */ unwrapOr(fallback: T2): T2; /** * ***Brief*** * Chains an task until the first `Err` is encountered. * * ***Example*** * ```ts * let result: Result<200n, 404n> = Err(404n); * result * .and(value => { * /// Task is skipped because `Result` is an `Err`. * /// ... * return Ok(value + 1n); * }) * .and(value => { * /// Task is skipped because `Result` is an `Err`. * /// ... * }); * ``` */ and(__: unknown): Err; /** * ***Brief*** * Transforms the `Ok` value if present, but if this is already an `Err`, it remains unchanged. * * ***Example*** * ```ts * let result0: Result<200n, 404n> = Ok(200n); * let result1: Result<201n, 404n> = result.map(value => { * /// Task is run because `Result` is `Ok`. * /// ... * return value + 1n; * }); * ``` */ map(__: unknown): Err; /** * ***Brief*** * Transforms the error contained in the `Err` using the provided `task` function. * * ***Example*** * ```ts * let result: Result<200n, 404n> = Err(404n); * result * .mapErr(e => { * return e + 1n; * }) * .inspect(); /// 405n. * ``` * * ***Example*** * ```ts * let result: Result<200n, 404n> = Ok(200n); * result * .mapErr(e => { * /// Task is run because `Result` is `Ok`. * /// ... * }); * ``` */ mapErr(task: Closure<[T1], T2>): Err; /** * ***Brief*** * Recovers from the current error by applying a recovery function, transforming the `Err` into an `Ok`. * * ***Example*** * ```ts * let result: Result<200n, 404n> = Err(404n); * let status: 200n = result * .recover(() => { * return 200n; * }) * .unlock(); * ``` */ recover(task: Closure<[T1], T2>): Ok; /** * ***Brief*** * Applies a transformation to the `Ok` value and returns an `Err` instance with the transformed value. * * ***Example*** * ```ts * let result: Result<200n, 404n> = Ok(200n); * let e: 404n = result * .degrade(() => { * return 404n; * }) * .inspect(); * console.log(e); /// 404n. * ``` */ degrade(__: unknown): Err; /** * ***Brief*** * Converts a `Result` to an `Option`. * * ***Example*** * ```ts * let result: Result<200n, 404n>; * let option: Option<200n> = result.toOption(); * ``` */ toOption(): Option; }; /** * ***Brief*** * The failed state of a `Result`. * * ***Warning*** * Any operation attempting to access a `Result` must safely handle the `Err` state or terminate with an error. */ declare function Err(_value: T1): Err; declare function Err(_value: T1, _handler: Error.Handler): Err; declare namespace Err { type From> = T1 extends Err ? Err : never; type FromAll>> = { [T2 in keyof T1]: T1[T2] extends Err ? From : never; }; type ValFrom> = T1 extends Err ? T2 : never; type ValFromAll>> = { [T2 in keyof T1]: ValFrom; }; } /** * ***Brief*** * Utility type for creating branded types with a unique string literal identifier `T1`. * * ***Example*** * ```ts * type Foo = * & BrandedStruct<"Foo"> * & { * foo: void; * }; * * type Bar = * & BrandedStruct<"Bar"> * & { * foo: void; * }; * * let union: Foo | Bar; * if (union.type === "Foo") { * /// ... * } * ``` */ type BrandedStruct = { /** * ***Brief*** * Type-level marker specifying the unique type identifier `T1`. * * ***Example*** * ```ts * type Foo = * & BrandedStruct<"Foo"> * & { * foo: void; * }; * * type Bar = * & BrandedStruct<"Bar"> * & { * foo: void; * }; * * let union: Foo | Bar; * if (union.type === "Foo") { * /// ... * } * ``` */ type: T1; }; declare namespace BrandedStruct { type Task = Closure<[value: BrandedStruct], void>; type Handler = { match(unknown: unknown): unknown is BrandedStruct; match(unknown: unknown, task: Task): unknown is BrandedStruct; match(unknown: unknown, brand: T1): unknown is BrandedStruct; match(unknown: unknown, brand: T1, task: Task): unknown is BrandedStruct; }; const Handler: Handler; } /** * ***Brief*** * A type-safety trait, enabling validation and transformation through user-defined type guard functions. * * ***Example*** * ```ts * let foo: Parsable; * foo * .parse((inst): inst is bigint => typeof inst === "bigint") * .map(int => { * /// ... * }); * ``` */ type Parsable = { /** * ***Brief*** * `parse` validates the value using a provided type guard function. * * ***Example*** * ```ts * let foo: Parsable; * foo * .parse((inst): inst is bigint => typeof inst === "bigint") * .map(int => { * /// ... * }); * ``` */ parse(guard: TypeGuard): Option; }; /** * ***Brief*** * Utility type for creating branded types with a unique string literal identifier `T1`. * * ***Example*** * ```ts * type Foo = * & Branded<"Foo"> * & { * foo(): void; * }; * * type Bar = * & Branded<"Bar"> * & { * foo(): void; * }; * * let union: Foo | Bar; * if (union.type() === "Foo") { * /// ... * } * ``` */ type Branded = { /** * ***Brief*** * Type-level marker method specifying the unique type identifier `T1`. * * ***Example*** * ```ts * type Foo = * & Branded<"Foo"> * & { * foo(): void; * }; * * type Bar = * & Branded<"Bar"> * & { * foo(): void; * }; * * let union: Foo | Bar; * if (union.type() === "Foo") { * /// ... * } * ``` */ type(): T1; }; declare namespace Branded { type Task = Closure<[value: Branded], void>; type Handler = { match(unknown: unknown): unknown is Branded; match(unknown: unknown, task: Task): unknown is Branded; match(unknown: unknown, brand: T1): unknown is Branded; match(unknown: unknown, brand: T1, task: Task): unknown is Branded; }; const Handler: Handler; } /** * ***Brief*** * A type alias for a `Closure` that supports asynchronous operations. * * ***Example*** * ```ts * const fetch: AsyncClosure<[string], unknown> = async (url: string) => /// ...; * ``` */ type AsyncClosure, T2> = Closure>; /** * ***Brief*** * Represents a generic function type that takes a single argument and returns a value. * * ***Example*** * ```ts * const addOne: Function = (x: bigint) => x += 1n; * ``` */ type Function$1 = (payload: T1) => T2; /** * **Note** * `function` that narrows the type of the provided value to type `T1`. * * **Example** * ```ts * let isNumber: TypeGuard; * let unknown: unknown; * if (isNumber(unknown)) { * /// ... * } * ``` */ type TypeGuard = (unknown: unknown) => unknown is T1; /** * ***Brief*** * A type alias for a `Function` that supports asynchronous operation. * * ***Example*** * ```ts * const fetch: AsyncFunction = async (url: string) => /// ...; * ``` */ type AsyncFunction = Function$1>; /** * ***Brief*** * Represents a callable function type that accepts an array of arguments and returns a specified type. * * ***Example*** * ```ts * const add: Closure<[bigint, bigint], bigint> = (x: bigint, y: bigint) => x + y; * ``` */ type Closure, T2> = (...payload: T1) => T2; /** * ***Brief*** * A value that can either be resolved immediately or asynchronously. */ type MaybeAsync = Promise | T1; type Dyn = Alloc | DeAlloc; /** * ***Brief*** * A dynamic resource management pattern, allowing for * allocation and deallocation of resources with proper lifecycle handling. * * ***Warning*** * The `Dyn` wrapper must be properly managed to avoid memory leaks. If the wrapper itself is garbage collected * without its content being explicitly deallocated, the resources it manages will not be recycled * and made available for reuse. Ensure all allocated resources are deallocated before the `Dyn` instance goes out of scope. * * ***Example*** * ```ts * type Car = { * drive(): void; * }; * * const Car: DynConstructor = Dyn( * (_model: string) => { * constructor { * return { drive }; * } * * function drive(): void { * return "Vroom."; * } * }, * car => { * /// Reset car or any tasks before it is made available again. * /// ... * return car; * }, 32n, "ModelT" * ); * * let car: Dyn = Car("ModelF"); * car = car.deAlloc(); * car.map(car => { * /// Will not run because `car` has been deallocated. * /// ... * }); * ``` * * ***Example*** * ```ts * /// Warning. * let car: Dyn = Car("ModelB"); * car.deAlloc(); * car.map(car => { * /// Will run because the car must be updated to the new state. * /// Always assign the `deAlloc` result a new `Dyn` wrapper or * /// itself. * /// ... * }); * ``` */ declare function Dyn>(_constructor: Closure, _onDeAlloc: Closure<[T1], T1>, _load: bigint, ..._onLoadPayload: T2): Dyn.Constructor; declare namespace Dyn { type Constructor> = Closure>; type Wrapper = { /** * ***Brief*** * Deallocates a resource, making it available for recycling. * * ***Example*** * ```ts * let personD: Dyn<{ name: string }>; * personD * .deAlloc() * .map(person => { * /// Not run because `person` was deallocated. * /// ... * }); * ``` */ deAlloc(): DeAlloc; }; } type Alloc = Dyn.Wrapper & Some & { /** * ***Brief*** * Unwraps the wrapped value of type `T1`. * * ***Requirement*** * Does not `panic`. * * ***Example*** * ```ts * let unsafe: Unsafe = Unsafe(500n); * unsafe.unwrap(); * ``` */ unwrap(): T1; }; declare function Alloc(_value: T1, _dyn: Dyn.Wrapper): Alloc; type DeAlloc = None & Dyn.Wrapper; /** * ***Brief*** * The deallocated state of an allocated resource. */ declare function DeAlloc(_dyn: Dyn.Wrapper): DeAlloc; declare namespace Ref { type Task = Closure<[new: T1, old: T1], void>; type DeletionTask = Closure<[], void>; } type Ref = { /** * ***Brief*** * Unwraps the wrapped value of type `T1`. * * ***Requirement*** * Does not `panic`. * * ***Example*** * ```ts * let unsafe: Unsafe = Unsafe(500n); * unsafe.unwrap(); * ``` */ unwrap(): T1; /** * ***Brief*** * Mutates the internal value of the `Ref` instance. */ mut(value: T1): Ref; /** * ***Brief*** * Registers a callback that is invoked whenever the value changes. * * ***Example*** * ```ts * let ref: Ref = Ref(200n); * ref.onChange(value => { * /// ... * return; * }); * ref.mut(404n); * ``` */ onChange(task: Ref.Task): Ref.DeletionTask; }; /** * ***Brief*** * Wrapper that supports mutation and change tracking. */ declare function Ref(_value: T1): Ref; export { Alloc, type AsyncClosure, type AsyncFunction, Branded, BrandedStruct, type Closure, DeAlloc, DomError, Dyn, Err, Error, Fpv, type Function$1 as Function, type MaybeAsync, None, Ok, Option, type Parsable, Ref, Result, Some, type TypeGuard, Unsafe, allO, allR, anyO, anyR, clone, flag, toString, wrap, wrapAsync };