import type { Predicate } from "./is.js"; export type AssertMessageFactory = (x: unknown, pred: Predicate, name?: string) => string; export declare const defaultAssertMessageFactory: AssertMessageFactory; /** * Represents an error that occurs when an assertion fails. */ export declare class AssertError extends Error { /** * Constructs a new `AssertError` instance. * @param message The error message. */ constructor(message?: string); } /** * Sets the factory function used to generate assertion error messages. * @param factory The factory function. * @example * ```ts * import { is, setAssertMessageFactory } from "@core/unknownutil"; * * setAssertMessageFactory((x, pred) => { * if (pred === is.String) { * return `Expected a string, got ${typeof x}`; * } else if (pred === is.Number) { * return `Expected a number, got ${typeof x}`; * } else if (pred === is.Boolean) { * return `Expected a boolean, got ${typeof x}`; * } else { * return `Expected a value that satisfies the predicate, got ${typeof x}`; * } * }); * ``` */ export declare function setAssertMessageFactory(factory: AssertMessageFactory): void; /** * Asserts that the given value satisfies the provided predicate. * * ```ts * import { assert, is } from "@core/unknownutil"; * * const a: unknown = "hello"; * assert(a, is.String); * // a is now narrowed to string * ``` * * @param x The value to be asserted. * @param pred The predicate function to test the value against. * @param options Optional configuration for the assertion. * @returns Nothing. The function has a return type of `asserts x is T` to help TypeScript narrow down the type of `x` after the assertion. * @throws {AssertError} If the value does not satisfy the predicate. */ export declare function assert(x: unknown, pred: Predicate, options?: { message?: string; name?: string; }): asserts x is T; /** * Ensures that the given value satisfies the provided predicate. * * ```ts * import { ensure, is } from "@core/unknownutil"; * * const a: unknown = "hello"; * const _: string = ensure(a, is.String); * ``` * * @param x The value to be ensured. * @param pred The predicate function to test the value against. * @param options Optional configuration for the assertion. * @returns The input value `x`. * @throws {AssertError} If the value does not satisfy the predicate. */ export declare function ensure(x: unknown, pred: Predicate, options?: { message?: string; name?: string; }): T; /** * Returns the input value if it satisfies the provided predicate, or `undefined` otherwise. * * ```ts * import { is, maybe } from "@core/unknownutil"; * * const a: unknown = "hello"; * const _: string = maybe(a, is.String) ?? "default value"; * ``` * * @param x The value to be tested. * @param pred The predicate function to test the value against. * @returns The input value `x` if it satisfies the predicate, or `undefined` otherwise. */ export declare function maybe(x: unknown, pred: Predicate): T | undefined; //# sourceMappingURL=util.d.ts.map