Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Apr 26, 2024
2 parents 78b0eeb + 1ba927c commit cbea73a
Show file tree
Hide file tree
Showing 208 changed files with 4,087 additions and 599 deletions.
6 changes: 3 additions & 3 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const A = Type.Object({

console.log(Byte)
console.log(TypeCompiler.Code(A))
console.log(Value.Check(A, { x: 0, y: 0, z: 0 }))
console.log(Value.Check(Byte, 255))
console.log(Value.Check(Byte, -1))
console.log(Value.Check(A, { x: 0, y: 0, z: 0 }))
console.log(Value.Check(Byte, 255))
console.log(Value.Check(Byte, -1))
console.log(Value.Check(Byte, NaN))

// Todo: Error Tests
Expand Down
48 changes: 48 additions & 0 deletions example/prototypes/mutual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*--------------------------------------------------------------------------
@sinclair/typebox/prototypes
The MIT License (MIT)
Copyright (c) 2017-2024 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

import { Type, TSchema, Static } from '@sinclair/typebox'

// Mutual Recursive Template
const __A = <T extends TSchema>(reference: T) => Type.Object({
b: Type.Union([reference, Type.Null()])
}, { additionalProperties: false })

const __B = <T extends TSchema>(reference: T) => Type.Object({
a: Type.Union([reference, Type.Null()])
}, { additionalProperties: false })

// ....

// Mutual Recursive Types
const A = Type.Recursive((This) => __A(__B(This)))
const B = Type.Recursive((This) => __B(__A(This)))

type A = Static<typeof A>;
type B = Static<typeof B>;

50 changes: 26 additions & 24 deletions example/prototypes/partial-deep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,40 @@ THE SOFTWARE.
import { TypeGuard, Type, TSchema, TIntersect, TUnion, TObject, TPartial, TProperties, Evaluate } from '@sinclair/typebox'

// -------------------------------------------------------------------------------------
// TDeepPartial
// TPartialDeepProperties
// -------------------------------------------------------------------------------------
export type TPartialDeepProperties<T extends TProperties> = {
[K in keyof T]: TPartial<T[K]>
[K in keyof T]: TPartialDeep<T[K]>
}
export type TPartialDeepRest<T extends TSchema[]> =
function PartialDeepProperties<T extends TProperties>(properties: T): TPartialDeepProperties<T> {
return Object.getOwnPropertyNames(properties).reduce((acc, key) => {
return {...acc, [key]: PartialDeep(properties[key])}
}, {}) as never
}
// -------------------------------------------------------------------------------------
// TPartialDeepRest
// -------------------------------------------------------------------------------------
export type TPartialDeepRest<T extends TSchema[], Acc extends TSchema[] = []> = (
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? [TPartial<L>, ...TPartialDeepRest<R>]
: []
? TPartialDeepRest<R, [...Acc, TPartialDeep<L>]>
: Acc
)
function PartialDeepRest<T extends TSchema[]>(rest: [...T]): TPartialDeepRest<T> {
return rest.map(schema => PartialDeep(schema)) as never
}
// -------------------------------------------------------------------------------------
// TPartialDeep
// -------------------------------------------------------------------------------------
export type TPartialDeep<T extends TSchema> =
T extends TIntersect<infer S> ? TIntersect<TPartialDeepRest<S>> :
T extends TUnion<infer S> ? TUnion<TPartialDeepRest<S>> :
T extends TObject<infer S> ? TPartial<TObject<Evaluate<TPartialDeepProperties<S>>>> :
T
// -------------------------------------------------------------------------------------
// DeepPartial
// -------------------------------------------------------------------------------------
function PartialDeepProperties<T extends TProperties>(properties: T) {
return Object.getOwnPropertyNames(properties).reduce((acc, key) => {
return {...acc, [key]: Type.Partial(properties[key])}
}, {} as TProperties)
}
function PartialDeepRest<T extends TSchema[]>(rest: [...T]): TPartialDeepRest<T> {
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<T extends TSchema>(type: T): TPartialDeep<T> {
export function PartialDeep<T extends TSchema>(schema: T): TPartialDeep<T> {
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
}
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.22",
"version": "0.32.25",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1791,11 +1791,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ CompiledMinifiedCompression
├──────────────────────┼────────────┼────────────┼─────────────┤
typebox/compiler'120.6 kb'' 52.9 kb''2.28 x'
typebox/errors' 55.7 kb'' 25.5 kb''2.19 x'
typebox/compiler'126.9 kb'' 55.7 kb''2.28 x'
typebox/errors' 46.1 kb'' 20.8 kb''2.22 x'
typebox/system' 4.7 kb'' 2.0 kb''2.33 x'
typebox/value'146.2 kb'' 62.0 kb''2.36 x'
typebox' 91.4 kb'' 37.8 kb''2.42 x'
typebox/value'152.2 kb'' 64.5 kb''2.36 x'
typebox' 95.7 kb'' 39.8 kb''2.40 x'
└──────────────────────┴────────────┴────────────┴─────────────┘
```
Expand Down
2 changes: 1 addition & 1 deletion src/type/any/any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ export interface TAny extends TSchema {

/** `[Json]` Creates an Any type */
export function Any(options: SchemaOptions = {}): TAny {
return { ...options, [Kind]: 'Any' } as unknown as TAny
return { ...options, [Kind]: 'Any' } as never
}
2 changes: 1 addition & 1 deletion src/type/array/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ export function Array<T extends TSchema>(schema: T, options: ArrayOptions = {}):
[Kind]: 'Array',
type: 'array',
items: CloneType(schema),
} as unknown as TArray<T>
} as never
}
2 changes: 1 addition & 1 deletion src/type/async-iterator/async-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ export function AsyncIterator<T extends TSchema>(items: T, options: SchemaOption
[Kind]: 'AsyncIterator',
type: 'AsyncIterator',
items: CloneType(items),
} as unknown as TAsyncIterator<T>
} as never
}
12 changes: 6 additions & 6 deletions src/type/awaited/awaited.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { CloneType } from '../clone/type'
// ------------------------------------------------------------------
// TypeGuard
// ------------------------------------------------------------------
import { IsIntersect, IsUnion, IsPromise } from '../guard/type'
import { IsIntersect, IsUnion, IsPromise } from '../guard/kind'
// ------------------------------------------------------------------
// FromRest
// ------------------------------------------------------------------
Expand All @@ -46,7 +46,7 @@ type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> =
: Acc
// prettier-ignore
function FromRest<T extends TSchema[]>(T: [...T]) : TFromRest<T> {
return T.map(L => AwaitedResolve(L)) as TFromRest<T>
return T.map(L => AwaitedResolve(L)) as never
}
// ----------------------------------------------------------------
// FromIntersect
Expand All @@ -55,7 +55,7 @@ function FromRest<T extends TSchema[]>(T: [...T]) : TFromRest<T> {
type TFromIntersect<T extends TSchema[]> = TIntersect<TFromRest<T>>
// prettier-ignore
function FromIntersect<T extends TSchema[]>(T: [...T]): TFromIntersect<T> {
return Intersect(FromRest(T) as TSchema[]) as unknown as TFromIntersect<T>
return Intersect(FromRest(T) as TSchema[]) as never
}
// ----------------------------------------------------------------
// FromUnion
Expand All @@ -64,15 +64,15 @@ function FromIntersect<T extends TSchema[]>(T: [...T]): TFromIntersect<T> {
type TFromUnion<T extends TSchema[]> = TUnion<TFromRest<T>>
// prettier-ignore
function FromUnion<T extends TSchema[]>(T: [...T]): TFromUnion<T> {
return Union(FromRest(T) as TSchema[]) as unknown as TFromUnion<T>
return Union(FromRest(T) as TSchema[]) as never
}
// ----------------------------------------------------------------
// Promise
// ----------------------------------------------------------------
type TFromPromise<T extends TSchema> = TAwaited<T>
// prettier-ignore
function FromPromise<T extends TSchema>(T: T): TFromPromise<T> {
return AwaitedResolve(T) as TFromPromise<T>
return AwaitedResolve(T) as never
}
// ----------------------------------------------------------------
// AwaitedResolve
Expand All @@ -84,7 +84,7 @@ function AwaitedResolve<T extends TSchema>(T: T): TAwaited<T> {
IsUnion(T) ? FromUnion(T.anyOf) :
IsPromise(T) ? FromPromise(T.item) :
T
) as TAwaited<T>
) as never
}
// ------------------------------------------------------------------
// TAwaited
Expand Down
2 changes: 1 addition & 1 deletion src/type/bigint/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ export function BigInt(options: BigIntOptions = {}): TBigInt {
...options,
[Kind]: 'BigInt',
type: 'bigint',
} as TBigInt
} as never
}
2 changes: 1 addition & 1 deletion src/type/boolean/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ export function Boolean(options: SchemaOptions = {}): TBoolean {
...options,
[Kind]: 'Boolean',
type: 'boolean',
} as unknown as TBoolean
} as never
}
2 changes: 1 addition & 1 deletion src/type/clone/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { Clone } from './value'

/** Clones a Rest */
export function CloneRest<T extends TSchema[]>(schemas: T): T {
return schemas.map((schema) => CloneType(schema)) as T
return schemas.map((schema) => CloneType(schema)) as never
}
/** Clones a Type */
export function CloneType<T extends TSchema>(schema: T, options: SchemaOptions = {}): T {
Expand Down
11 changes: 8 additions & 3 deletions src/type/clone/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ function RegExpType(value: RegExp) {
return new RegExp(value.source, value.flags)
}
function ObjectType(value: Record<keyof any, unknown>) {
const clonedProperties = Object.getOwnPropertyNames(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {})
const clonedSymbols = Object.getOwnPropertySymbols(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key as any]) }), {})
return { ...clonedProperties, ...clonedSymbols }
const result = {} as Record<PropertyKey, unknown>
for (const key of Object.getOwnPropertyNames(value)) {
result[key] = Visit(value[key])
}
for (const key of Object.getOwnPropertySymbols(value)) {
result[key] = Visit(value[key])
}
return result
}
// prettier-ignore
function Visit(value: unknown): any {
Expand Down
25 changes: 13 additions & 12 deletions src/type/composite/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { SetDistinct, TSetDistinct } from '../sets/index'
// ------------------------------------------------------------------
// TypeGuard
// ------------------------------------------------------------------
import { IsNever } from '../guard/type'
import { IsNever } from '../guard/kind'
// ------------------------------------------------------------------
// CompositeKeys
// ------------------------------------------------------------------
Expand All @@ -50,9 +50,9 @@ type TCompositeKeys<T extends TSchema[], Acc extends PropertyKey[] = []> = (
)
// prettier-ignore
function CompositeKeys<T extends TSchema[]>(T: [...T]): TCompositeKeys<T> {
return SetDistinct(T.reduce((Acc, L) => {
return ([...Acc, ...KeyOfPropertyKeys(L)]) as never
}, [])) as never
const Acc = [] as PropertyKey[]
for(const L of T) Acc.push(...KeyOfPropertyKeys(L))
return SetDistinct(Acc) as never
}
// ------------------------------------------------------------------
// FilterNever
Expand Down Expand Up @@ -80,9 +80,9 @@ type TCompositeProperty<T extends TSchema[], K extends PropertyKey, Acc extends
)
// prettier-ignore
function CompositeProperty<T extends TSchema[], K extends PropertyKey>(T: [...T], K: K): TCompositeProperty<T, K> {
return FilterNever(T.reduce((Acc, L) => {
return [...Acc, ...IndexFromPropertyKeys(L, [K])] as never
}, [])) as never
const Acc = [] as TSchema[]
for(const L of T) Acc.push(...IndexFromPropertyKeys(L, [K]))
return FilterNever(Acc) as never
}
// ------------------------------------------------------------------
// CompositeProperties
Expand All @@ -95,9 +95,11 @@ type TCompositeProperties<T extends TSchema[], K extends PropertyKey[], Acc = {}
)
// prettier-ignore
function CompositeProperties<T extends TSchema[], K extends PropertyKey[] = []>(T: [...T], K: [...K]): TCompositeProperties<T, K> {
return K.reduce((Acc, L) => {
return { ...Acc, [L]: IntersectEvaluated(CompositeProperty(T, L)) }
}, {}) as never
const Acc = {} as never
for(const L of K) {
Acc[L] = IntersectEvaluated(CompositeProperty(T, L))
}
return Acc
}
// ------------------------------------------------------------------
// Composite
Expand All @@ -111,11 +113,10 @@ type TCompositeEvaluate<
> = R
// prettier-ignore
export type TComposite<T extends TSchema[]> = TCompositeEvaluate<T>

// prettier-ignore
export function Composite<T extends TSchema[]>(T: [...T], options: ObjectOptions = {}): TComposite<T> {
const K = CompositeKeys(T)
const P = CompositeProperties(T, K)
const R = Object(P, options)
return R as TComposite<T>
return R as never
}
14 changes: 7 additions & 7 deletions src/type/const/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type TFromArray<T extends readonly unknown[]> =
: T
// prettier-ignore
function FromArray<T extends readonly unknown[]>(T: [...T]): TFromArray<T> {
return T.map(L => FromValue(L, false)) as TFromArray<T>
return T.map(L => FromValue(L, false)) as never
}
// ------------------------------------------------------------------
// FromProperties
Expand All @@ -73,16 +73,16 @@ type TFromProperties<T extends Record<PropertyKey, unknown>> = {
}
// prettier-ignore
function FromProperties<T extends Record<PropertyKey, unknown>>(value: T): TFromProperties<T> {
return globalThis.Object.getOwnPropertyNames(value).reduce((acc, key) => {
return { ...acc, [key]: Readonly(FromValue(value[key], false)) }
}, {} as TProperties) as unknown as TFromProperties<T>
const Acc = {} as TProperties
for(const K of globalThis.Object.getOwnPropertyNames(value)) Acc[K] = Readonly(FromValue(value[K], false))
return Acc as never
}
// ------------------------------------------------------------------
// ConditionalReadonly - Only applied if not root
// ------------------------------------------------------------------
type TConditionalReadonly<T extends TSchema, Root extends boolean> = Root extends true ? T : TReadonly<T>
function ConditionalReadonly<T extends TSchema, Root extends boolean>(T: T, root: Root): TConditionalReadonly<T, Root> {
return (root === true ? T : Readonly(T)) as unknown as TConditionalReadonly<T, Root>
return (root === true ? T : Readonly(T)) as never
}
// ------------------------------------------------------------------
// FromValue
Expand Down Expand Up @@ -122,7 +122,7 @@ function FromValue<T, Root extends boolean>(value: T, root: Root): FromValue<T,
IsBoolean(value) ? Literal(value) :
IsString(value) ? Literal(value) :
Object({})
) as FromValue<T, Root>
) as never
}
// ------------------------------------------------------------------
// TConst
Expand All @@ -131,5 +131,5 @@ export type TConst<T> = FromValue<T, true>

/** `[JavaScript]` Creates a readonly const type from the given value. */
export function Const</* const (not supported in 4.0) */ T>(T: T, options: SchemaOptions = {}): TConst<T> {
return CloneType(FromValue(T, true), options) as TConst<T>
return CloneType(FromValue(T, true), options) as never
}

0 comments on commit cbea73a

Please sign in to comment.