import type { JsonSchema } from "./JsonSchema.js"; /** * Extracts the inferred TypeScript type from a JsonSchema. * * This utility type enables type-safe schema usage by extracting the type parameter * from JsonSchema instances. * * ### Usage * * ```typescript * const userSchema = JsonSchema.from({ type: "object" }); * type User = Infer; // Extracts the type * ``` * * @typeParam S - The JsonSchema to extract the type from * * @public */ export type Infer = S extends JsonSchema ? T : never; /** * Maps an object of JsonSchema properties to a concrete TypeScript type. * * Converts a record of JsonSchema properties into a TypeScript type where each * property has the inferred type from its corresponding schema. * * ### Usage * * ```typescript * const properties = { * name: string(), * age: number(), * email: string().format("email") * }; * * type Shape = PropsToShape; * // Result: { name: string; age: number; email: string } * ``` * * @typeParam P - Record of property names to JsonSchema instances * * @public */ export type PropsToShape

>> = { [K in keyof P]: Infer; }; type JsonLikeObject = Record; type ObjectPortion = Extract; type NonObjectPortion = Exclude; type ObjectPick = Pick, Extract>>; type ObjectOmit = Omit, Extract>>; type ObjectPartial = { [K in keyof ObjectPortion]?: ObjectPortion[K] | undefined; }; type MergedObjectPortion = [ObjectPortion] extends [never] ? ObjectPortion : [ObjectPortion] extends [never] ? ObjectPortion : ObjectPortion & ObjectPortion; export type SchemaPick = [ObjectPortion] extends [never] ? T : NonObjectPortion extends never ? ObjectPick : ObjectPick | NonObjectPortion; export type SchemaOmit = [ObjectPortion] extends [never] ? T : NonObjectPortion extends never ? ObjectOmit : ObjectOmit | NonObjectPortion; export type SchemaPartial = [ObjectPortion] extends [never] ? T : NonObjectPortion extends never ? ObjectPartial : ObjectPartial | NonObjectPortion; export type SchemaMerge = [MergedObjectPortion] extends [never] ? NonObjectPortion | NonObjectPortion : NonObjectPortion extends never ? NonObjectPortion extends never ? MergedObjectPortion : MergedObjectPortion | NonObjectPortion : MergedObjectPortion | NonObjectPortion | NonObjectPortion; type ObjectKeys = ObjectPortion extends never ? never : keyof ObjectPortion; export type SchemaKey = ObjectKeys extends never ? string : Extract, string>; /** * Maps common JavaScript constructors to their corresponding TypeScript types. * * This utility type converts constructor functions (String, Number, Date, etc.) into * their corresponding runtime types, enabling better type inference when creating * schemas programmatically. * * ### Type Mappings * * - `StringConstructor` → `string` * - `NumberConstructor` → `number` * - `BooleanConstructor` → `boolean` * - `DateConstructor` → `Date` * - `ArrayConstructor` → `any[]` * - `MapConstructor` → `Record` * - `SetConstructor` → `Set` * - `ObjectConstructor` → `Record` * - Custom classes → `InstanceType` * * ### Usage * * ```typescript * type StringType = CtorToType; // string * type DateType = CtorToType; // Date * type CustomType = CtorToType; // MyClass * ``` * * @typeParam C - The constructor function to map * * @public */ export type CtorToType = C extends StringConstructor ? string : C extends NumberConstructor ? number : C extends BooleanConstructor ? boolean : C extends DateConstructor ? Date : C extends ArrayConstructor ? any[] : C extends MapConstructor ? Record : C extends SetConstructor ? Set : C extends ObjectConstructor ? Record : C extends abstract new (...args: any) => any ? InstanceType : any; /** * Converts a union type to an intersection type. * * This advanced TypeScript utility transforms a union of types (A | B | C) into * an intersection of types (A & B & C), which is useful for schema composition * and merging operations. * * ### Usage * * ```typescript * type Union = { a: string } | { b: number }; * type Intersection = UnionToIntersection; * // Result: { a: string } & { b: number } * ``` * * @typeParam U - The union type to convert * * @public */ export type UnionToIntersection = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; export {};