diff --git a/example/prototypes/partial-deep.ts b/example/prototypes/partial-deep.ts index 7ee36f44..5fc707f8 100644 --- a/example/prototypes/partial-deep.ts +++ b/example/prototypes/partial-deep.ts @@ -29,38 +29,40 @@ THE SOFTWARE. import { TypeGuard, Type, TSchema, TIntersect, TUnion, TObject, TPartial, TProperties, Evaluate } from '@sinclair/typebox' // ------------------------------------------------------------------------------------- -// TDeepPartial +// TPartialDeepProperties // ------------------------------------------------------------------------------------- export type TPartialDeepProperties = { - [K in keyof T]: TPartial + [K in keyof T]: TPartialDeep } -export type TPartialDeepRest = +function PartialDeepProperties(properties: T): TPartialDeepProperties { + return Object.getOwnPropertyNames(properties).reduce((acc, key) => { + return {...acc, [key]: PartialDeep(properties[key])} + }, {}) as never +} +// ------------------------------------------------------------------------------------- +// TPartialDeepRest +// ------------------------------------------------------------------------------------- +export type TPartialDeepRest = ( T extends [infer L extends TSchema, ...infer R extends TSchema[]] - ? [TPartial, ...TPartialDeepRest] - : [] + ? TPartialDeepRest]> + : Acc +) +function PartialDeepRest(rest: [...T]): TPartialDeepRest { + return rest.map(schema => PartialDeep(schema)) as never +} +// ------------------------------------------------------------------------------------- +// TPartialDeep +// ------------------------------------------------------------------------------------- export type TPartialDeep = T extends TIntersect ? TIntersect> : T extends TUnion ? TUnion> : T extends TObject ? TPartial>>> : T -// ------------------------------------------------------------------------------------- -// DeepPartial -// ------------------------------------------------------------------------------------- -function PartialDeepProperties(properties: T) { - return Object.getOwnPropertyNames(properties).reduce((acc, key) => { - return {...acc, [key]: Type.Partial(properties[key])} - }, {} as TProperties) -} -function PartialDeepRest(rest: [...T]): TPartialDeepRest { - const [L, ...R] = rest - return (R.length > 0) ? [Type.Partial(L), ...PartialDeepRest(R)] : [] as any -} -/** Maps the given schema as deep partial, making all properties and sub properties optional */ -export function PartialDeep(type: T): TPartialDeep { +export function PartialDeep(schema: T): TPartialDeep { return ( - TypeGuard.IsIntersect(type) ? Type.Intersect(PartialDeepRest(type.allOf)) : - TypeGuard.IsUnion(type) ? Type.Union(PartialDeepRest(type.anyOf)) : - TypeGuard.IsObject(type) ? Type.Partial(Type.Object(PartialDeepProperties(type.properties))) : - type - ) as any + TypeGuard.IsIntersect(schema) ? Type.Intersect(PartialDeepRest(schema.allOf)) : + TypeGuard.IsUnion(schema) ? Type.Union(PartialDeepRest(schema.anyOf)) : + TypeGuard.IsObject(schema) ? Type.Partial(Type.Object(PartialDeepProperties(schema.properties))) : + schema + ) as never } \ No newline at end of file