Skip to content

Commit

Permalink
Revision 0.32.10 (#734)
Browse files Browse the repository at this point in the history
* Export Partial and Required Type Infrastructure

* Version
  • Loading branch information
sinclairzx81 authored Jan 17, 2024
1 parent dcf48f3 commit 0c0da45
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.32.9",
"version": "0.32.10",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
26 changes: 12 additions & 14 deletions src/type/partial/partial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import { IsMappedResult, IsIntersect, IsUnion, IsObject } from '../guard/type'
// prettier-ignore
type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> = (
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? TFromRest<R, [...Acc, PartialResolve<L>]>
? TFromRest<R, [...Acc, TPartial<L>]>
: Acc
)
// prettier-ignore
Expand Down Expand Up @@ -80,27 +80,25 @@ function FromProperties<T extends TProperties>(T: T) {
// PartialResolve
// ------------------------------------------------------------------
// prettier-ignore
type PartialResolve<T extends TSchema> = (
T extends TRecursive<infer S> ? TRecursive<PartialResolve<S>> :
T extends TIntersect<infer S> ? TIntersect<TFromRest<S>> :
T extends TUnion<infer S> ? TUnion<TFromRest<S>> :
T extends TObject<infer S> ? TObject<TFromProperties<S>> :
TObject<{}>
)
// prettier-ignore
function PartialResolve<T extends TSchema>(T: T): PartialResolve<T> {
function PartialResolve<T extends TSchema>(T: T): TPartial<T> {
return (
IsIntersect(T) ? Intersect(FromRest(T.allOf)) :
IsUnion(T) ? Union(FromRest(T.anyOf)) :
IsObject(T) ? Object(FromProperties(T.properties)) :
Object({})
) as PartialResolve<T>
) as TPartial<T>
}
// ------------------------------------------------------------------
// TPartial
// ------------------------------------------------------------------
export type TPartial<T extends TSchema> = PartialResolve<T>

// prettier-ignore
export type TPartial<T extends TSchema> = (
T extends TRecursive<infer S> ? TRecursive<TPartial<S>> :
T extends TIntersect<infer S> ? TIntersect<TFromRest<S>> :
T extends TUnion<infer S> ? TUnion<TFromRest<S>> :
T extends TObject<infer S> ? TObject<TFromProperties<S>> :
TObject<{}>
)
/** `[Json]` Constructs a type where all properties are optional */
export function Partial<T extends TMappedResult>(T: T, options?: SchemaOptions): TPartialFromMappedResult<T>
/** `[Json]` Constructs a type where all properties are optional */
Expand All @@ -110,5 +108,5 @@ export function Partial(T: TSchema, options: SchemaOptions = {}): any {
if (IsMappedResult(T)) return PartialFromMappedResult(T, options)
const D = Discard(T, [TransformKind, '$id', 'required']) as TSchema
const R = CloneType(PartialResolve(T), options)
return { ...D, ...R } as any
return { ...D, ...R } as never
}
25 changes: 12 additions & 13 deletions src/type/required/required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { IsMappedResult, IsIntersect, IsUnion, IsObject } from '../guard/type'
// prettier-ignore
type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> = (
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? TFromRest<R, [...Acc, TRequiredResolve<L>]>
? TFromRest<R, [...Acc, TRequired<L>]>
: Acc
)
// prettier-ignore
Expand All @@ -80,28 +80,27 @@ function FromProperties<T extends TProperties>(T: T) {
// ------------------------------------------------------------------
// RequiredResolve
// ------------------------------------------------------------------

// prettier-ignore
type TRequiredResolve<T extends TSchema> = (
T extends TRecursive<infer S> ? TRecursive<TRequiredResolve<S>> :
T extends TIntersect<infer S> ? TIntersect<TFromRest<S>> :
T extends TUnion<infer S> ? TUnion<TFromRest<S>> :
T extends TObject<infer S> ? TObject<TFromProperties<S>> :
TObject<{}>
)
// prettier-ignore
function RequiredResolve<T extends TSchema>(T: T): TRequiredResolve<T> {
function RequiredResolve<T extends TSchema>(T: T): TRequired<T> {
return (
IsIntersect(T) ? Intersect(FromRest(T.allOf)) :
IsUnion(T) ? Union(FromRest(T.anyOf)) :
IsObject(T) ? Object(FromProperties(T.properties)) :
Object({})
) as TRequiredResolve<T>
) as TRequired<T>
}
// ------------------------------------------------------------------
// TRequired
// ------------------------------------------------------------------
export type TRequired<T extends TSchema> = TRequiredResolve<T>

// prettier-ignore
export type TRequired<T extends TSchema> = (
T extends TRecursive<infer S> ? TRecursive<TRequired<S>> :
T extends TIntersect<infer S> ? TIntersect<TFromRest<S>> :
T extends TUnion<infer S> ? TUnion<TFromRest<S>> :
T extends TObject<infer S> ? TObject<TFromProperties<S>> :
TObject<{}>
)
/** `[Json]` Constructs a type where all properties are required */
export function Required<T extends TMappedResult>(T: T, options?: SchemaOptions): TRequiredFromMappedResult<T>
/** `[Json]` Constructs a type where all properties are required */
Expand Down

0 comments on commit 0c0da45

Please sign in to comment.