zod-to-x
Version:
Multi language types generation from Zod schemas.
31 lines (30 loc) • 1.54 kB
TypeScript
type TClass = new (...args: any[]) => any;
/**
* Converts a union type `U` into an intersection type.
*
* This utility type works by leveraging conditional types and function inference.
* It transforms each member of the union into a function parameter and then infers
* the intersection of all parameter types.
*
* @template U - The union type to be converted into an intersection type.
*
* @example
* type Union = { a: string } | { b: number };
* type Intersection = TUnionToIntersection<Union>;
* // Result: { a: string } & { b: number }
*/
type TUnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
/**
* Combines multiple derived classes with a base class into a single class.
*
* @template T - A type extending `TClass`, representing the derived classes.
* @template K - A type extending `TClass`, representing the base class.
* @param DerivedClasses - An array of derived classes to be combined.
* @param BaseClass - The base class to be extended.
* @returns A new class that combines the functionality of the derived classes
* and the base class. The resulting class has the constructor of the
* base class and the combined instance types of all derived classes.
* @throws {Error} If no derived classes are provided.
*/
export declare function Zod2XMixin<T extends TClass, K extends TClass>(DerivedClasses: T[], BaseClass: K): new (...args: ConstructorParameters<K>) => InstanceType<K> & TUnionToIntersection<InstanceType<T>>;
export {};