/* ============================================================================= * UNIFIED BASE SCHEMA MAPPER SYSTEM * ============================================================================= */ import type { ContainerShape, DocShape, Shape } from "./shape.js" // Helper type for depth counting in ExpandDeep type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] /** * Expands a type to a reasonable depth for IDE display. * Stops at depth 4 to avoid infinite recursion with self-referential types * like tree nodes that have `children: TreeNodeJSON[]`. * * @example * ```typescript * // Without expansion, hover shows: TreeNodeJSON> * // With expansion, hover shows the actual structure: * // { id: string; parent: string | null; data: { name: string; ... }; children: ... } * ``` */ type ExpandDeep = Depth extends 0 ? T : T extends (...args: any[]) => any ? T : T extends object ? T extends infer O ? { [K in keyof O]: ExpandDeep } : never : T /** * Infers the plain (JSON-serializable) type from any Shape. * The result is fully expanded for better IDE hover display. * * This is the recommended way to extract types from shapes. * Works with DocShape, ContainerShape, and ValueShape. * * @example * ```typescript * const ChatSchema = Shape.doc({ * messages: Shape.list(Shape.struct({ * id: Shape.plain.string(), * content: Shape.text(), * })), * }) * * // Extract the document type * type ChatDoc = Infer * // Result: { messages: { id: string; content: string }[] } * * const PresenceSchema = Shape.plain.struct({ * name: Shape.plain.string(), * cursor: Shape.plain.struct({ x: Shape.plain.number(), y: Shape.plain.number() }), * }) * * // Extract the presence type * type Presence = Infer * // Result: { name: string; cursor: { x: number; y: number } } * ``` */ export type Infer = T extends Shape ? ExpandDeep

: never /** * Infers the plain (JSON-serializable) type from any Shape without expansion. * Use this if you prefer to see type alias names (like TreeNodeJSON) in hover displays, * or if you need slightly faster type checking on very large schemas. */ export type InferRaw = T extends Shape ? P : never /** * Infers the mutable type from any Shape. * This is the type used within change() callbacks for mutation. */ export type InferMutableType = T extends Shape ? M : never /** * @deprecated Use InferMutableType instead */ export type InferDraftType = InferMutableType /** * Extracts the valid placeholder type from a shape. * * For dynamic containers (list, record, etc.), this will be constrained to * empty values ([] or {}) to prevent users from expecting per-entry merging. */ export type InferPlaceholderType = T extends Shape ? P : never /** * Mutable type for use within change() callbacks and direct mutations on doc.value. * This is the type-safe wrapper around CRDT containers that allows mutation. */ export type Mutable>> = InferMutableType /** * @deprecated Use Mutable instead. Draft is an alias kept for backwards compatibility. */ export type Draft>> = Mutable /** * Interface for objects that have a toJSON method. * This is separate from the data type to avoid polluting Object.values(). */ export interface HasToJSON { toJSON(): T }