Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revision 0.32.25 #849

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>;

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.24",
"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 @@ -1804,11 +1804,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ Compiled │ Minified │ Compression │
├──────────────────────┼────────────┼────────────┼─────────────┤
│ 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
}
2 changes: 1 addition & 1 deletion src/type/constructor/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ export function Constructor<T extends TSchema[], U extends TSchema>(parameters:
type: 'Constructor',
parameters: CloneRest(parameters),
returns: CloneType(returns),
} as unknown as TConstructor<T, U>
} as never
}
2 changes: 1 addition & 1 deletion src/type/date/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ export function Date(options: DateOptions = {}): TDate {
...options,
[Kind]: 'Date',
type: 'Date',
} as unknown as TDate
} as never
}
16 changes: 9 additions & 7 deletions src/type/deref/deref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { IsUndefined } from '../guard/value'
// ------------------------------------------------------------------
// TypeGuard
// ------------------------------------------------------------------
import { IsConstructor, IsFunction, IsIntersect, IsUnion, IsTuple, IsArray, IsObject, IsPromise, IsAsyncIterator, IsIterator, IsRef } from '../guard/type'
import { IsConstructor, IsFunction, IsIntersect, IsUnion, IsTuple, IsArray, IsObject, IsPromise, IsAsyncIterator, IsIterator, IsRef } from '../guard/kind'
// ------------------------------------------------------------------
// FromRest
// ------------------------------------------------------------------
Expand All @@ -56,8 +56,8 @@ export type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> = (
? TFromRest<R, [...Acc, TDeref<L>]>
: Acc
)
function FromRest(schema: TSchema[], references: TSchema[]) {
return schema.map((schema) => Deref(schema, references))
function FromRest<T extends TSchema[]>(schema: [...T], references: TSchema[]): TFromRest<T> {
return schema.map((schema) => Deref(schema, references)) as never
}
// ------------------------------------------------------------------
// FromProperties
Expand All @@ -68,9 +68,11 @@ type FromProperties<T extends TProperties> = Evaluate<{
}>
// prettier-ignore
function FromProperties(properties: TProperties, references: TSchema[]) {
return globalThis.Object.getOwnPropertyNames(properties).reduce((acc, key) => {
return {...acc, [key]: Deref(properties[key], references) }
}, {} as TProperties)
const Acc = {} as TProperties
for(const K of globalThis.Object.getOwnPropertyNames(properties)) {
Acc[K] = Deref(properties[K], references)
}
return Acc as never
}
// prettier-ignore
function FromConstructor(schema: TConstructor, references: TSchema[]) {
Expand Down Expand Up @@ -147,7 +149,7 @@ function DerefResolve<T extends TSchema>(schema: T, references: TSchema[]): TDer
IsIterator(schema) ? FromIterator(schema, references) :
IsRef(schema) ? FromRef(schema, references) :
schema
) as TDeref<T>
) as never
}
// prettier-ignore
export type TDeref<T extends TSchema> =
Expand Down
2 changes: 1 addition & 1 deletion src/type/enum/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ export function Enum<V extends TEnumValue, T extends Record<TEnumKey, V>>(item:
.map((key) => item[key]) as T[keyof T][]
const values2 = [...new Set(values1)]
const anyOf = values2.map((value) => Literal(value))
return Union(anyOf, { ...options, [Hint]: 'Enum' }) as unknown as TEnum<T>
return Union(anyOf, { ...options, [Hint]: 'Enum' }) as never
}
Loading
Loading