Skip to content

Commit

Permalink
Revision 0.32.6 (#725)
Browse files Browse the repository at this point in the history
* Export Deref Infrastructure

* Remove Resolve type for Deref, Omit, Pick and Awaited

* Export Index Infrastructure

* Readme

* Reorder Template Literal Exports
  • Loading branch information
sinclairzx81 authored Jan 10, 2024
1 parent 4c0b764 commit ed234d0
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 96 deletions.
22 changes: 11 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type T = Static<typeof T> // type T = {
TypeBox is a runtime type builder that creates in-memory Json Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type that can be statically checked by TypeScript and runtime asserted using standard Json Schema validation.
This library is built to be a runtime type system offering similar capabilities to TypeScript's static type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
This library is designed to be a runtime type system providing similar capabilities to TypeScript's programmable type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
License MIT
Expand Down Expand Up @@ -648,7 +648,7 @@ TypeBox provides an extended type set that can be used to create schematics for
### Import
Import the Type namespace to bring in TypeBox's full type system. This is recommended for most users.
Import the Type namespace to bring in the full TypeBox type system. This is recommended for most users.
```typescript
import { Type, type Static } from '@sinclair/typebox'
Expand Down Expand Up @@ -870,7 +870,7 @@ function test(node: Node) {
### Template Literal Types
TypeBox supports template literal types with TemplateLiteral. This type can be created using a syntax similar to the TypeScript template literal syntax or composed from exterior types. TypeBox encodes template literals as regular expressions which enables the template to be checked by Json Schema validators. This type also supports regular expression parsing that enables template patterns to be used for generative types. The following shows both TypeScript and TypeBox usage.
TypeBox supports template literal types with the TemplateLiteral function. This type can be created using a syntax similar to the TypeScript template literal syntax or composed from exterior types. TypeBox encodes template literals as regular expressions which enables the template to be checked by Json Schema validators. This type also supports regular expression parsing that enables template patterns to be used for generative types. The following shows both TypeScript and TypeBox usage.
```typescript
// TypeScript
Expand Down Expand Up @@ -905,7 +905,7 @@ const R = Type.Record(K, Type.String()) // const R: TObject<{
### Indexed Access Types
TypeBox supports indexed access types with Index. This type enables uniform access to interior property and element types without having to extract them from the underlying schema representation. This type is supported for Object, Array, Tuple, Union and Intersect types.
TypeBox supports indexed access types with the Index function. This function enables uniform access to interior property and element types without having to extract them from the underlying schema representation. Index types are supported for Object, Array, Tuple, Union and Intersect types.
```typescript
const T = Type.Object({ // type T = {
Expand Down Expand Up @@ -944,7 +944,7 @@ const C = Type.Index(T, Type.KeyOf(T)) // type C = T[keyof T]
### Mapped Types
TypeBox supports mapped object types with Mapped. This type accepts two arguments, the first is a union type typically derived from KeyOf, the second is a mapping function that receives a mapping key `K` that can be used to index properties of a type. The following implements Partial using mapped types.
TypeBox supports mapped types with the Mapped function. This function accepts two arguments, the first is a union type typically derived from KeyOf, the second is a mapping function that receives a mapping key `K` that can be used to index properties of a type. The following implements a mapped type that remaps each property to be `T | null`
```typescript
const T = Type.Object({ // type T = {
Expand All @@ -953,22 +953,22 @@ const T = Type.Object({ // type T = {
z: Type.Boolean() // z: boolean
}) // }

const M = Type.Mapped(Type.KeyOf(T), K => { // type M = { [K in keyof T]?: T[K] }
return Type.Optional(Type.Index(T, K)) //
const M = Type.Mapped(Type.KeyOf(T), K => { // type M = { [K in keyof T]: T[K] | null }
return Type.Union([Type.Index(T, K), Type.Null()]) //
}) // ... evaluated as
//
// const M: TObject<{
// x: TOptional<TNumber>,
// y: TOptional<TString>,
// z: TOptional<TBoolean>
// x: TUnion<[TNumber, TNull]>,
// y: TUnion<[TString, TNull]>,
// z: TUnion<[TBoolean, TNull]>
// }>
```
<a name='types-conditional'></a>
### Conditional Types
TypeBox supports runtime conditional types with Extends. This type performs a structural assignability check against the first (`left`) and second (`right`) arguments and will return either the third (`true`) or fourth (`false`) argument based on the result. The conditional types Exclude and Extract are also supported. The following shows both TypeScript and TypeBox examples of conditional types.
TypeBox supports runtime conditional types with the Extends function. This function performs a structural assignability check against the first (`left`) and second (`right`) arguments and will return either the third (`true`) or fourth (`false`) argument based on the result. The conditional types Exclude and Extract are also supported. The following shows both TypeScript and TypeBox examples of conditional types.
```typescript
// Extends
Expand Down
33 changes: 23 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export { TypeBoxError } from './type/error/index'
export { Any, type TAny } from './type/any/index'
export { Array, type TArray, type ArrayOptions } from './type/array/index'
export { AsyncIterator, type TAsyncIterator } from './type/async-iterator/index'
export { Awaited, type TAwaited, type TAwaitedResolve } from './type/awaited/index'
export { Awaited, type TAwaited } from './type/awaited/index'
export { BigInt, type TBigInt, type BigIntOptions } from './type/bigint/index'
export { Boolean, type TBoolean } from './type/boolean/index'
export { Composite, type TComposite } from './type/composite/index'
Expand All @@ -56,7 +56,20 @@ export { Extends, ExtendsCheck, ExtendsResult, ExtendsUndefinedCheck, type TExte
export { Extract, type TExtract, type TExtractFromMappedResult } from './type/extract/index'
export { Function, type TFunction } from './type/function/index'
export { Increment, type Assert, type AssertType, type AssertRest, type AssertProperties, type Ensure, type Evaluate, type TupleToIntersect, type TupleToUnion, type UnionToTuple } from './type/helpers/index'
export { Index, IndexPropertyKeys, IndexFromMappedKey, IndexFromMappedResult, type TIndex, type TIndexPropertyKeys, type TIndexFromMappedKey, type TIndexFromMappedResult } from './type/indexed/index'
export {
Index,
IndexPropertyKeys,
IndexFromPropertyKeys,
IndexFromPropertyKey,
IndexFromMappedKey,
IndexFromMappedResult,
type TIndex,
type TIndexPropertyKeys,
type TIndexFromPropertyKeys,
type TIndexFromPropertyKey,
type TIndexFromMappedKey,
type TIndexFromMappedResult,
} from './type/indexed/index'
export { InstanceType, type TInstanceType } from './type/instance-type/index'
export { Integer, type TInteger, type IntegerOptions } from './type/integer/index'
export { Intersect, IntersectEvaluated, type TIntersect, type TIntersectEvaluated, type IntersectOptions } from './type/intersect/index'
Expand All @@ -70,11 +83,11 @@ export { Not, type TNot } from './type/not/index'
export { Null, type TNull } from './type/null/index'
export { Number, type TNumber, type NumberOptions } from './type/number/index'
export { Object, type TObject, type TProperties, type ObjectOptions } from './type/object/index'
export { Omit, type TOmit, type TOmitResolve, type TOmitFromMappedKey, type TOmitFromMappedResult } from './type/omit/index'
export { Omit, type TOmit, type TOmitFromMappedKey, type TOmitFromMappedResult } from './type/omit/index'
export { Optional, OptionalFromMappedResult, type TOptional, type TOptionalWithFlag, type TOptionalFromMappedResult } from './type/optional/index'
export { Parameters, type TParameters } from './type/parameters/index'
export { Partial, PartialFromMappedResult, type TPartial, type TPartialFromMappedResult } from './type/partial/index'
export { Pick, type TPick, type TPickResolve, type TPickFromMappedKey, type TPickFromMappedResult } from './type/pick/index'
export { Pick, type TPick, type TPickFromMappedKey, type TPickFromMappedResult } from './type/pick/index'
export { Promise, type TPromise } from './type/promise/index'
export { Readonly, ReadonlyFromMappedResult, type TReadonly, type TReadonlyWithFlag, type TReadonlyFromMappedResult } from './type/readonly/index'
export { ReadonlyOptional, type TReadonlyOptional } from './type/readonly-optional/index'
Expand All @@ -92,18 +105,18 @@ export { String, type TString, type StringOptions, type StringFormatOption, type
export { Symbol, type TSymbol, type TSymbolValue as SymbolValue } from './type/symbol/index'
export {
TemplateLiteral,
IsTemplateLiteralFinite,
IsTemplateLiteralExpressionFinite,
TemplateLiteralSyntax,
TemplateLiteralGenerate,
TemplateLiteralParse,
TemplateLiteralParseExact,
TemplateLiteralGenerate,
IsTemplateLiteralFinite,
TemplateLiteralExpressionGenerate,
TemplateLiteralSyntax,
type TTemplateLiteralSyntax,
IsTemplateLiteralExpressionFinite,
type TTemplateLiteral,
type TIsTemplateLiteralFinite,
type TTemplateLiteralSyntax,
type TTemplateLiteralGenerate,
type TTemplateLiteralKind,
type TIsTemplateLiteralFinite,
} from './type/template-literal/index'
export { Transform, TransformDecodeBuilder, TransformEncodeBuilder, type TTransform, type TransformOptions, type TransformFunction } from './type/transform/index'
export { Tuple, type TTuple } from './type/tuple/index'
Expand Down
26 changes: 11 additions & 15 deletions src/type/awaited/awaited.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { IsIntersect, IsUnion, IsPromise } 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, TAwaitedResolve<L>]>
? TFromRest<R, [...Acc, TAwaited<L>]>
: Acc
// prettier-ignore
function FromRest<T extends TSchema[]>(T: [...T]) : TFromRest<T> {
Expand All @@ -69,37 +69,33 @@ function FromUnion<T extends TSchema[]>(T: [...T]): TFromUnion<T> {
// ----------------------------------------------------------------
// Promise
// ----------------------------------------------------------------
type TFromPromise<T extends TSchema> = TAwaitedResolve<T>
type TFromPromise<T extends TSchema> = TAwaited<T>
// prettier-ignore
function FromPromise<T extends TSchema>(T: T): TFromPromise<T> {
return AwaitedResolve(T) as TFromPromise<T>
}
// ----------------------------------------------------------------
// FromSchema
// AwaitedResolve
// ----------------------------------------------------------------
// prettier-ignore
export type TAwaitedResolve<T extends TSchema> =
T extends TIntersect<infer S> ? TIntersect<TFromRest<S>> :
T extends TUnion<infer S> ? TUnion<TFromRest<S>> :
T extends TPromise<infer S> ? TAwaitedResolve<S> :
T
// prettier-ignore
export function AwaitedResolve<T extends TSchema>(T: T): TAwaitedResolve<T> {
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 TAwaitedResolve<T>
) as TAwaited<T>
}
// ------------------------------------------------------------------
// TAwaited
// ------------------------------------------------------------------
// prettier-ignore
export type TAwaited<T extends TSchema> = (
TAwaitedResolve<T>
)
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
/** `[JavaScript]` Constructs a type by recursively unwrapping Promise types */
export function Awaited<T extends TSchema>(T: T, options: SchemaOptions = {}): TAwaitedResolve<T> {
export function Awaited<T extends TSchema>(T: T, options: SchemaOptions = {}): TAwaited<T> {
return CloneType(AwaitedResolve(T), options)
}
36 changes: 17 additions & 19 deletions src/type/deref/deref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { IsConstructor, IsFunction, IsIntersect, IsUnion, IsTuple, IsArray, IsOb
// prettier-ignore
export type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> = (
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? TFromRest<R, [...Acc, DerefResolve<L>]>
? TFromRest<R, [...Acc, TDeref<L>]>
: Acc
)
function FromRest(schema: TSchema[], references: TSchema[]) {
Expand All @@ -64,7 +64,7 @@ function FromRest(schema: TSchema[], references: TSchema[]) {
// ------------------------------------------------------------------
// prettier-ignore
type FromProperties<T extends TProperties> = Evaluate<{
[K in keyof T]: DerefResolve<T[K]>
[K in keyof T]: TDeref<T[K]>
}>
// prettier-ignore
function FromProperties(properties: TProperties, references: TSchema[]) {
Expand Down Expand Up @@ -133,21 +133,7 @@ function FromRef(schema: TRef, references: TSchema[]) {
return Deref(discard, references)
}
// prettier-ignore
export type DerefResolve<T extends TSchema> =
T extends TConstructor<infer S extends TSchema[], infer R extends TSchema> ? TConstructor<TFromRest<S>, DerefResolve<R>> :
T extends TFunction<infer S extends TSchema[], infer R extends TSchema> ? TFunction<TFromRest<S>, DerefResolve<R>> :
T extends TIntersect<infer S extends TSchema[]> ? TIntersect<TFromRest<S>> :
T extends TUnion<infer S extends TSchema[]> ? TUnion<TFromRest<S>> :
T extends TTuple<infer S extends TSchema[]> ? TTuple<TFromRest<S>> :
T extends TObject<infer S extends TProperties> ? TObject<FromProperties<S>> :
T extends TArray<infer S extends TSchema> ? TArray<DerefResolve<S>> :
T extends TPromise<infer S extends TSchema> ? TPromise<DerefResolve<S>> :
T extends TAsyncIterator<infer S extends TSchema> ? TAsyncIterator<DerefResolve<S>> :
T extends TIterator<infer S extends TSchema> ? TIterator<DerefResolve<S>> :
T extends TRef<infer S extends TSchema> ? DerefResolve<S> :
T
// prettier-ignore
export function DerefResolve<T extends TSchema>(schema: T, references: TSchema[]): TDeref<T> {
function DerefResolve<T extends TSchema>(schema: T, references: TSchema[]): TDeref<T> {
return (
IsConstructor(schema) ? FromConstructor(schema, references) :
IsFunction(schema) ? FromFunction(schema, references) :
Expand All @@ -163,11 +149,23 @@ export function DerefResolve<T extends TSchema>(schema: T, references: TSchema[]
schema
) as TDeref<T>
}
// prettier-ignore
export type TDeref<T extends TSchema> =
T extends TConstructor<infer S extends TSchema[], infer R extends TSchema> ? TConstructor<TFromRest<S>, TDeref<R>> :
T extends TFunction<infer S extends TSchema[], infer R extends TSchema> ? TFunction<TFromRest<S>, TDeref<R>> :
T extends TIntersect<infer S extends TSchema[]> ? TIntersect<TFromRest<S>> :
T extends TUnion<infer S extends TSchema[]> ? TUnion<TFromRest<S>> :
T extends TTuple<infer S extends TSchema[]> ? TTuple<TFromRest<S>> :
T extends TObject<infer S extends TProperties> ? TObject<FromProperties<S>> :
T extends TArray<infer S extends TSchema> ? TArray<TDeref<S>> :
T extends TPromise<infer S extends TSchema> ? TPromise<TDeref<S>> :
T extends TAsyncIterator<infer S extends TSchema> ? TAsyncIterator<TDeref<S>> :
T extends TIterator<infer S extends TSchema> ? TIterator<TDeref<S>> :
T extends TRef<infer S extends TSchema> ? TDeref<S> :
T
// ------------------------------------------------------------------
// TDeref
// ------------------------------------------------------------------
export type TDeref<T extends TSchema> = DerefResolve<T>

/** `[Json]` Creates a dereferenced type */
export function Deref<T extends TSchema>(schema: T, references: TSchema[]): TDeref<T> {
return DerefResolve(CloneType(schema), CloneRest(references))
Expand Down
Loading

0 comments on commit ed234d0

Please sign in to comment.