import { type Maybe } from '../value/maybe.type'; /** * A primitive value that survives a JSON round trip unchanged. */ export type JsonPrimitive = string | number | boolean | null; /** * A JSON object whose values are all {@link JsonValue}s. */ export interface JsonObject { readonly [key: string]: JsonValue | undefined; } /** * Any value that survives a JSON round trip (`JSON.parse(JSON.stringify(value))`) unchanged. */ export type JsonValue = JsonPrimitive | readonly JsonValue[] | JsonObject; /** * Types that never survive a JSON round trip. {@link JsonCompatible} maps them to `never`. */ export type JsonIncompatibleType = Date | RegExp | Map | Set | WeakMap | WeakSet | Promise | ((...args: never[]) => unknown) | bigint | symbol; /** * Maps T to the closest type that survives a JSON round trip. * * Properties that hold a {@link JsonIncompatibleType} (a `Date`, a function, a `Map`, etc.) are mapped to `never`, so a value of type T only * satisfies `JsonCompatible` when every property is JSON-compatible. Use it as an F-bounded constraint (`T extends JsonCompatible`) or as an * intersection on a parameter (`value: T & JsonCompatible`). * * `unknown`/`any` map to {@link JsonValue}. * * Class instances can't be told apart from plain objects at compile time. {@link findNonJsonCompatibleValue} is the authoritative runtime check. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilTags json, serializable, compatible, type, round-trip * @dbxUtilRelated find-non-json-compatible-value, is-json-compatible-value */ export type JsonCompatible = unknown extends T ? JsonValue : T extends JsonPrimitive | undefined ? T : T extends JsonIncompatibleType ? never : T extends readonly unknown[] ? { [K in keyof T]: JsonCompatible; } : T extends object ? { [K in keyof T]: JsonCompatible; } : never; /** * Reason a value fails a JSON round trip. */ export type JsonIncompatibilityReason = 'function' | 'symbol' | 'bigint' | 'non_finite_number' | 'undefined_in_array' | 'date' | 'regexp' | 'map' | 'set' | 'class_instance' | 'circular'; /** * The first value found that fails a JSON round trip, and where it was found. */ export interface JsonIncompatibility { /** * Path to the offending value, e.g. `a.b[2].c`. Empty for the root value. */ readonly path: string; readonly reason: JsonIncompatibilityReason; } /** * Finds the first value inside the input that would not survive a JSON round trip (`JSON.parse(JSON.stringify(value))`) unchanged. * * Rejects functions, symbols, bigints, `NaN`/`Infinity`, `Date`/`RegExp`/`Map`/`Set` values, class instances, `undefined` array items and circular references. * `undefined` object properties are allowed, since they are dropped and the round-tripped value is equivalent. * * @param value - The value to check. * @returns The first incompatibility with its path, or undefined when the value is JSON-compatible. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilTags json, serializable, compatible, check, validate, round-trip, path * @dbxUtilRelated is-json-compatible-value * * @example * ```ts * findNonJsonCompatibleValue({ a: [1, { b: new Date() }] }); * // { path: 'a[1].b', reason: 'date' } * ``` */ export declare function findNonJsonCompatibleValue(value: unknown): Maybe; /** * Whether or not the value survives a JSON round trip unchanged. See {@link findNonJsonCompatibleValue}. * * @param value - The value to check. * @returns True when the value is JSON-compatible. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilTags json, serializable, compatible, check, is, round-trip * @dbxUtilRelated find-non-json-compatible-value */ export declare function isJsonCompatibleValue(value: T): value is T & JsonCompatible; /** * Returns a human-readable message for a {@link JsonIncompatibility}. * * @param incompatibility - The incompatibility to describe. * @returns A message such as `"a[1].b" is a Date, which is not JSON-compatible.` */ export declare function jsonIncompatibilityMessage(incompatibility: JsonIncompatibility): string;