Skip to content

Commit

Permalink
Revision 0.34.1 (#1080)
Browse files Browse the repository at this point in the history
* Implement Computed Deref Types for Import

* ChangeLog
  • Loading branch information
sinclairzx81 authored Nov 16, 2024
1 parent 53c3d75 commit 56b760c
Show file tree
Hide file tree
Showing 46 changed files with 2,075 additions and 1,072 deletions.
4 changes: 4 additions & 0 deletions changelog/0.34.0.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 0.34.0
- [Revision 0.34.1](https://github.com/sinclairzx81/typebox/pull/1080)
- Implement Computed Type Deref in Modules

## [0.34.0](https://www.npmjs.com/package/@sinclair/typebox/v/0.34.0)

## Overview
Expand Down
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.34.0",
"version": "0.34.1",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
42 changes: 26 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1074,22 +1074,32 @@ const T = Type.Object({ // const T: TObject<{
### Module
Syntax Types support Module parsing, which is useful for processing multiple TypeScript types. Module parsing supports type alias and interface definitions. Generics are currently unsupported as of 0.34.0.
Syntax Types also support Module parsing. This can provide a more terse syntax for creating Module definitions, but comes with an inference performance cost. Module parsing supports interface and type alias definitions. Generics types are currently unsupported.
```typescript
const Foo = Parse(`module Foo {
export type A = string
export type B = number
export type C = A | B
const Module = Parse(`module {
export interface User {
id: string
name: string
email: string
}
export type PartialUser = (
Pick<User, 'id'> &
Partial<Omit<User, 'id'>>
)
}`)

const C = Foo.Import('C') // const C: TImport<{
// ...
// }, 'C'>
const PartialUser = Module.Import('PartialUser') // TImport<{...}, 'PartialUser'>

type PartialUser = Static<typeof PartialUser> // type PartialUser = {
// id: string,
// } & {
// name?: string,
// email?: string,
// }
```
<a name='syntax-context'></a>
Expand Down Expand Up @@ -1883,12 +1893,12 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ CompiledMinifiedCompression
├──────────────────────┼────────────┼────────────┼─────────────┤
typebox/compiler'121.7 kb'' 53.4 kb''2.28 x'
typebox/errors' 75.3 kb'' 33.4 kb''2.25 x'
typebox/syntax'120.1 kb'' 50.5 kb''2.38 x'
typebox/compiler'122.4 kb'' 53.4 kb''2.29 x'
typebox/errors' 67.6 kb'' 29.6 kb''2.28 x'
typebox/syntax'132.9 kb'' 54.2 kb''2.45 x'
typebox/system' 7.4 kb'' 3.2 kb''2.33 x'
typebox/value'160.3 kb'' 67.4 kb''2.38 x'
typebox' 96.2 kb'' 40.2 kb''2.39 x'
typebox/value'150.1 kb'' 62.2 kb''2.41 x'
typebox'106.8 kb'' 43.2 kb''2.47 x'
└──────────────────────┴────────────┴────────────┴─────────────┘
```
Expand Down
5 changes: 3 additions & 2 deletions src/syntax/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,12 @@ const FactorIndexArray = (Type: Types.TSchema, IndexArray: unknown[]): Types.TSc
const [Left, Right] = DestructureRight(IndexArray) as [unknown[], Types.TSchema[]]
return (
!Types.ValueGuard.IsUndefined(Right) ? (
Right.length === 1 ? Types.Index(FactorIndexArray(Type, Left), Right[0]) :
// note: Indexed types require reimplementation to replace `[number]` indexers
Right.length === 1 ? Types.Index(FactorIndexArray(Type, Left), Right[0]) as never :
Right.length === 0 ? Types.Array(FactorIndexArray(Type, Left)) :
Types.Never()
) : Type
)
)
}
// prettier-ignore
const FactorMapping = (KeyOf: boolean, Type: Types.TSchema, IndexArray: unknown[], Extends: Types.TSchema[]) => {
Expand Down
10 changes: 5 additions & 5 deletions src/syntax/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ type Partial = Static.Tuple<[
// prettier-ignore
interface RequiredMapping extends Static.IMapping {
output: this['input'] extends ['Required', LAngle, infer Type extends Types.TSchema, RAngle]
? Types.TPartial<Type>
? Types.TRequired<Type>
: never
}
// prettier-ignore
Expand All @@ -788,8 +788,8 @@ type Required = Static.Tuple<[
// ------------------------------------------------------------------
// prettier-ignore
interface PickMapping extends Static.IMapping {
output: this['input'] extends ['Pick', LAngle, infer Type extends Types.TSchema, Comma, infer PropertyKey extends Types.TSchema, RAngle]
? Types.TPick<Type, Types.TIndexPropertyKeys<PropertyKey>>
output: this['input'] extends ['Pick', LAngle, infer Type extends Types.TSchema, Comma, infer Key extends Types.TSchema, RAngle]
? Types.TPick<Type, Key>
: never
}
// prettier-ignore
Expand All @@ -801,8 +801,8 @@ type Pick = Static.Tuple<[
// ------------------------------------------------------------------
// prettier-ignore
interface OmitMapping extends Static.IMapping {
output: this['input'] extends ['Omit', LAngle, infer Type extends Types.TSchema, Comma, infer PropertyKey extends Types.TSchema, RAngle]
? Types.TOmit<Type, Types.TIndexPropertyKeys<PropertyKey>>
output: this['input'] extends ['Omit', LAngle, infer Type extends Types.TSchema, Comma, infer Key extends Types.TSchema, RAngle]
? Types.TOmit<Type, Key>
: never
}
// prettier-ignore
Expand Down
2 changes: 1 addition & 1 deletion src/type/array/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ export interface TArray<T extends TSchema = TSchema> extends TSchema, ArrayOptio
items: T
}
/** `[Json]` Creates an Array type */
export function Array<T extends TSchema>(items: T, options?: ArrayOptions): TArray<T> {
export function Array<Type extends TSchema>(items: Type, options?: ArrayOptions): TArray<Type> {
return CreateType({ [Kind]: 'Array', type: 'array', items }, options) as never
}
94 changes: 57 additions & 37 deletions src/type/awaited/awaited.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,76 +26,96 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { CreateType } from '../create/type'
import { Ensure } from '../helpers/index'
import type { TSchema, SchemaOptions } from '../schema/index'
import { Computed, type TComputed } from '../computed/index'
import { Intersect, type TIntersect } from '../intersect/index'
import { Union, type TUnion } from '../union/index'
import { type TPromise } from '../promise/index'
import { CreateType } from '../create/type'
import { Ref, type TRef } from '../ref/index'

// ------------------------------------------------------------------
// TypeGuard
// ------------------------------------------------------------------
import { IsIntersect, IsUnion, IsPromise } from '../guard/kind'
// ------------------------------------------------------------------
// FromRest
// ------------------------------------------------------------------
import { IsIntersect, IsUnion, IsPromise, IsRef, IsComputed } from '../guard/kind'

// ----------------------------------------------------------------
// FromComputed
// ----------------------------------------------------------------
// prettier-ignore
type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> =
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? TFromRest<R, [...Acc, TAwaited<L>]>
: Acc
type TFromComputed<Target extends string, Parameters extends TSchema[]> = Ensure<(
TComputed<'Awaited', [TComputed<Target, Parameters>]>
)>
// prettier-ignore
function FromComputed<Target extends string, Parameters extends TSchema[]>(target: Target, parameters: Parameters): TFromComputed<Target, Parameters> {
return Computed('Awaited', [Computed(target, parameters)]) as never
}
// ----------------------------------------------------------------
// Ref
// ----------------------------------------------------------------
type TFromRef<Ref extends string> = Ensure<TComputed<'Awaited', [TRef<Ref>]>>
// prettier-ignore
function FromRest<T extends TSchema[]>(T: [...T]) : TFromRest<T> {
return T.map(L => AwaitedResolve(L)) as never
function FromRef<Ref extends string>($ref: Ref): TFromRef<Ref> {
return Computed('Awaited', [Ref($ref)]) as never
}
// ----------------------------------------------------------------
// FromIntersect
// ----------------------------------------------------------------
// prettier-ignore
type TFromIntersect<T extends TSchema[]> = TIntersect<TFromRest<T>>
type TFromIntersect<Types extends TSchema[]> = (
TIntersect<TFromRest<Types>>
)
// prettier-ignore
function FromIntersect<T extends TSchema[]>(T: [...T]): TFromIntersect<T> {
return Intersect(FromRest(T) as TSchema[]) as never
function FromIntersect<Types extends TSchema[]>(types: [...Types]): TFromIntersect<Types> {
return Intersect(FromRest(types) as TSchema[]) as never
}
// ----------------------------------------------------------------
// FromUnion
// ----------------------------------------------------------------
// prettier-ignore
type TFromUnion<T extends TSchema[]> = TUnion<TFromRest<T>>
type TFromUnion<Types extends TSchema[]> = TUnion<TFromRest<Types>>
// prettier-ignore
function FromUnion<T extends TSchema[]>(T: [...T]): TFromUnion<T> {
return Union(FromRest(T) as TSchema[]) as never
function FromUnion<Types extends TSchema[]>(types: [...Types]): TFromUnion<Types> {
return Union(FromRest(types) as TSchema[]) as never
}
// ----------------------------------------------------------------
// Promise
// ----------------------------------------------------------------
type TFromPromise<T extends TSchema> = TAwaited<T>
type TFromPromise<Type extends TSchema> = TAwaited<Type>
// prettier-ignore
function FromPromise<T extends TSchema>(T: T): TFromPromise<T> {
return AwaitedResolve(T) as never
function FromPromise<Type extends TSchema>(type: Type): TFromPromise<Type> {
return Awaited(type) as never
}
// ----------------------------------------------------------------
// AwaitedResolve
// ----------------------------------------------------------------
// ------------------------------------------------------------------
// FromRest
// ------------------------------------------------------------------
// prettier-ignore
function AwaitedResolve<T extends TSchema>(T: T): TAwaited<T> {
return (
IsIntersect(T) ? FromIntersect(T.allOf) :
IsUnion(T) ? FromUnion(T.anyOf) :
IsPromise(T) ? FromPromise(T.item) :
T
) as never
type TFromRest<Types extends TSchema[], Result extends TSchema[] = []> = (
Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]]
? TFromRest<Right, [...Result, TAwaited<Left>]>
: Result
)
// prettier-ignore
function FromRest<Types extends TSchema[]>(types: [...Types]) : TFromRest<Types> {
return types.map(type => Awaited(type)) as never
}
// ------------------------------------------------------------------
// TAwaited
// ------------------------------------------------------------------
// prettier-ignore
export type TAwaited<T extends TSchema> =
T extends TIntersect<infer S> ? TIntersect<TFromRest<S>> :
T extends TUnion<infer S> ? TUnion<TFromRest<S>> :
T extends TPromise<infer S> ? TAwaited<S> :
T
export type TAwaited<Type extends TSchema> = (
Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<Target, Parameters> :
Type extends TRef<infer Ref extends string> ? TFromRef<Ref> :
Type extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromRest<Types>> :
Type extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromRest<Types>> :
Type extends TPromise<infer Type extends TSchema> ? TAwaited<Type> :
Type
)
/** `[JavaScript]` Constructs a type by recursively unwrapping Promise types */
export function Awaited<T extends TSchema>(T: T, options?: SchemaOptions): TAwaited<T> {
return CreateType(AwaitedResolve(T), options) as never
export function Awaited<T extends TSchema>(type: T, options?: SchemaOptions): TAwaited<T> {
return CreateType(
IsComputed(type) ? FromComputed(type.target, type.parameters) : IsIntersect(type) ? FromIntersect(type.allOf) : IsUnion(type) ? FromUnion(type.anyOf) : IsPromise(type) ? FromPromise(type.item) : IsRef(type) ? FromRef(type.$ref) : type,
options,
) as never
}
44 changes: 44 additions & 0 deletions src/type/computed/computed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*--------------------------------------------------------------------------
@sinclair/typebox/type
The MIT License (MIT)
Copyright (c) 2017-2024 Haydn Paterson (sinclair) <[email protected]>
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, SchemaOptions } from '../schema/index'
import { CreateType } from '../create/index'
import { Kind } from '../symbols/symbols'

// ------------------------------------------------------------------
// Computed
// ------------------------------------------------------------------
export interface TComputed<Target extends string = string, Parameters extends TSchema[] = []> extends TSchema {
[Kind]: 'Computed'
target: Target
parameters: Parameters
}
/** `[Internal]` Creates a deferred computed type. This type is used exclusively in modules to defer resolution of computable types that contain interior references */
export function Computed<Target extends string, Parameters extends TSchema[]>(target: Target, parameters: [...Parameters], options?: SchemaOptions): TComputed<Target, Parameters> {
return CreateType({ [Kind]: 'Computed', target, parameters }, options) as never
}
29 changes: 29 additions & 0 deletions src/type/computed/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*--------------------------------------------------------------------------
@sinclair/typebox/type
The MIT License (MIT)
Copyright (c) 2017-2024 Haydn Paterson (sinclair) <[email protected]>
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.
---------------------------------------------------------------------------*/

export * from './computed'
Loading

0 comments on commit 56b760c

Please sign in to comment.