@alphanova/builder
Version:
A fully fledged facade that facilitates object manipulation
45 lines (44 loc) • 1.71 kB
TypeScript
type LayoutElementObject<S extends Record<string, LayoutElement<boolean>>, D extends object = object> = {
type: 'object';
default: D;
schema: S;
stripId?: boolean;
};
type LayoutBase<B extends true | false> = {
optional?: B;
nullState?: 'present' | 'undefined';
onMismatch?: 'warn' | 'error' | 'ignore';
onMissing?: 'warn' | 'error' | 'ignore';
};
export type LayoutElement<B extends true | false> = LayoutBase<B> & ({
type: 'string';
default: string;
min_length?: number;
max_length?: number;
} | {
type: 'array';
default: unknown[];
elements: LayoutElement<boolean>;
min_length?: number;
max_length?: number;
} | {
type: 'number';
default: number;
onString?: 'warn' | 'error' | 'ignore' | 'convert' | 'round' | 'floor' | 'ceil';
min?: number;
max?: number;
} | {
type: 'date';
default: Date;
min?: Date;
max?: Date;
} | {
type: 'boolean';
default: boolean;
} | LayoutElementObject<Record<string, any>>);
export type Layout<TModel, B extends true | false> = Record<keyof TModel, LayoutElement<B>>;
type InferredType<S extends Record<string, LayoutElement<boolean>>> = {
[K in keyof S]: S[K]['type'] extends 'object' ? object : S[K]['type'] extends 'string' ? string : S[K]['type'] extends 'array' ? unknown[] : S[K]['type'] extends 'number' ? number : S[K]['type'] extends 'date' ? Date : S[K]['type'] extends 'boolean' ? boolean : never;
};
export declare function createObjectLayout<B extends true | false, S extends Record<string, LayoutElement<boolean>>>(element: LayoutBase<B> & LayoutElementObject<S, InferredType<S>>): LayoutBase<B> & LayoutElementObject<S, InferredType<S>>;
export {};