Skip to content

Commit

Permalink
Revision 0.32.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 28, 2023
1 parent ddfe887 commit dffcb5f
Show file tree
Hide file tree
Showing 79 changed files with 354 additions and 364 deletions.
7 changes: 7 additions & 0 deletions changelog/0.32.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## [0.32.0](https://www.npmjs.com/package/@sinclair/typebox/v/0.32.0)

## Overview

Revision 0.32.0 is milestone revision for the TypeBox project. This revision focuses on the modularization of all TypeBox types, reducing TypeBuilder size and reimplementing much of the TypeBox core to provide better inference stability for both indexed access types, composite types as well as to make provisions for ESM publishing in future.

This revision passes tests for TypeBox's public API, however internal type names related to inference resolution have been removed in this revision. This constitutes a minor breaking change.
2 changes: 1 addition & 1 deletion examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TypeSystem } from '@sinclair/typebox/system'
import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Value, ValuePointer } from '@sinclair/typebox/value'
import { Type, TypeGuard, Symbols, Static, TSchema } from '@sinclair/typebox'
import { Type, TypeGuard, Kind, Static, TSchema } from '@sinclair/typebox'

// -----------------------------------------------------------
// Create: Type
Expand Down
6 changes: 3 additions & 3 deletions examples/prototypes/union-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TypeRegistry, Symbols, TSchema, SchemaOptions } from '@sinclair/typebox'
import { TypeRegistry, Kind, TSchema, SchemaOptions } from '@sinclair/typebox'

// -------------------------------------------------------------------------------------
// TUnionEnum
// -------------------------------------------------------------------------------------
export interface TUnionEnum<T extends (string | number)[]> extends TSchema {
[Symbols.Kind]: 'UnionEnum'
[Kind]: 'UnionEnum'
static: T[number]
enum: T
}
Expand All @@ -45,5 +45,5 @@ export function UnionEnum<T extends (string | number)[]>(values: [...T], options
return (typeof value === 'string' || typeof value === 'number') && schema.enum.includes(value)
}
if (!TypeRegistry.Has('UnionEnum')) TypeRegistry.Set('UnionEnum', UnionEnumCheck)
return { ...options, [Symbols.Kind]: 'UnionEnum', enum: values } as TUnionEnum<T>
return { ...options, [Kind]: 'UnionEnum', enum: values } as TUnionEnum<T>
}
6 changes: 3 additions & 3 deletions examples/prototypes/union-oneof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TypeRegistry, Symbols, Static, TSchema, SchemaOptions } from '@sinclair/typebox'
import { TypeRegistry, Kind, Static, TSchema, SchemaOptions } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

// -------------------------------------------------------------------------------------
// TUnionOneOf
// -------------------------------------------------------------------------------------
export interface TUnionOneOf<T extends TSchema[]> extends TSchema {
[Symbols.Kind]: 'UnionOneOf'
[Kind]: 'UnionOneOf'
static: { [K in keyof T]: Static<T[K]> }[number]
oneOf: T
}
Expand All @@ -46,5 +46,5 @@ export function UnionOneOf<T extends TSchema[]>(oneOf: [...T], options: SchemaOp
return 1 === schema.oneOf.reduce((acc: number, schema: any) => (Value.Check(schema, value) ? acc + 1 : acc), 0)
}
if (!TypeRegistry.Has('UnionOneOf')) TypeRegistry.Set('UnionOneOf', UnionOneOfCheck)
return { ...options, [Symbols.Kind]: 'UnionOneOf', oneOf } as TUnionOneOf<T>
return { ...options, [Kind]: 'UnionOneOf', oneOf } as TUnionOneOf<T>
}
104 changes: 52 additions & 52 deletions examples/typedef/typedef.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1309,8 +1309,8 @@ import { TypeRegistry, Symbols } from '@sinclair/typebox'

TypeRegistry.Set('Foo', (schema, value) => value === 'foo')

const A = Value.Check({ [Symbols.Kind]: 'Foo' }, 'foo') // const A = true
const B = Value.Check({ [Symbols.Kind]: 'Foo' }, 'bar') // const B = false
const A = Value.Check({ [Kind]: 'Foo' }, 'foo') // const A = true
const B = Value.Check({ [Kind]: 'Foo' }, 'bar') // const B = false
```
<a name='typeregistry-format'></a>
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export namespace TypeCompiler {
// Guards
// ----------------------------------------------------------------------
function IsAnyOrUnknown(schema: Types.TSchema) {
return schema[Types.Symbols.Kind] === 'Any' || schema[Types.Symbols.Kind] === 'Unknown'
return schema[Types.Kind] === 'Any' || schema[Types.Kind] === 'Unknown'
}
// -------------------------------------------------------------------
// Types
Expand Down Expand Up @@ -397,7 +397,7 @@ export namespace TypeCompiler {
function* TKind(schema: Types.TSchema, references: Types.TSchema[], value: string): IterableIterator<string> {
const instance = state.instances.size
state.instances.set(instance, schema)
yield `kind('${schema[Types.Symbols.Kind]}', ${instance}, ${value})`
yield `kind('${schema[Types.Kind]}', ${instance}, ${value})`
}
function* Visit<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: string, useHoisting: boolean = true): IterableIterator<string> {
const references_ = IsString(schema.$id) ? [...references, schema] : references
Expand All @@ -415,7 +415,7 @@ export namespace TypeCompiler {
return yield `${functionName}(${value})`
}
}
switch (schema_[Types.Symbols.Kind]) {
switch (schema_[Types.Kind]) {
case 'Any':
return yield* TAny(schema_, references_, value)
case 'Array':
Expand Down Expand Up @@ -477,7 +477,7 @@ export namespace TypeCompiler {
case 'Void':
return yield* TVoid(schema_, references_, value)
default:
if (!Types.TypeRegistry.Has(schema_[Types.Symbols.Kind])) throw new TypeCompilerUnknownTypeError(schema)
if (!Types.TypeRegistry.Has(schema_[Types.Kind])) throw new TypeCompilerUnknownTypeError(schema)
return yield* TKind(schema_, references_, value)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,13 @@ function* TVoid(schema: Types.TVoid, references: Types.TSchema[], path: string,
if (!TypeSystemPolicy.IsVoidLike(value)) yield Create(ValueErrorType.Void, schema, path, value)
}
function* TKind(schema: Types.TSchema, references: Types.TSchema[], path: string, value: any): IterableIterator<ValueError> {
const check = Types.TypeRegistry.Get(schema[Types.Symbols.Kind])!
const check = Types.TypeRegistry.Get(schema[Types.Kind])!
if (!check(schema, value)) yield Create(ValueErrorType.Kind, schema, path, value)
}
function* Visit<T extends Types.TSchema>(schema: T, references: Types.TSchema[], path: string, value: any): IterableIterator<ValueError> {
const references_ = IsDefined<string>(schema.$id) ? [...references, schema] : references
const schema_ = schema as any
switch (schema_[Types.Symbols.Kind]) {
switch (schema_[Types.Kind]) {
case 'Any':
return yield* TAny(schema_, references_, path, value)
case 'Array':
Expand Down Expand Up @@ -534,7 +534,7 @@ function* Visit<T extends Types.TSchema>(schema: T, references: Types.TSchema[],
case 'Void':
return yield* TVoid(schema_, references_, path, value)
default:
if (!Types.TypeRegistry.Has(schema_[Types.Symbols.Kind])) throw new ValueErrorsUnknownTypeError(schema)
if (!Types.TypeRegistry.Has(schema_[Types.Kind])) throw new ValueErrorsUnknownTypeError(schema)
return yield* TKind(schema_, references_, path, value)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export { type TRest, Rest } from './type/rest/index'
export { type TReturnType, ReturnType } from './type/return-type/index'
export { type TSchema, type SchemaOptions } from './type/schema/index'
export { type Static, type StaticDecode, type StaticEncode } from './type/static/index'
export { Strict } from './type/strict/index'
export { type TString, type StringOptions, type StringFormatOption, type StringContentEncodingOption, String } from './type/string/index'
export { type TSymbol, type SymbolValue, Symbol } from './type/symbol/index'
export { type TTemplateLiteral, type TTemplateLiteralKind, TemplateLiteral } from './type/template-literal/index'
Expand All @@ -92,12 +93,11 @@ export { type TVoid, Void } from './type/void/index'
// ------------------------------------------------------------------
// Infrastructure
// ------------------------------------------------------------------
export { Kind, Hint, ReadonlyKind, OptionalKind, TransformKind } from './type/symbols/index'
export { TypeRegistry, FormatRegistry } from './type/registry/index'
export { TypeGuard, ValueGuard } from './type/guard/index'
export { CloneType, CloneRest } from './type/clone/type'
export { Clone } from './type/clone/value'
export { Strict } from './type/strict/index'
export { Symbols } from './type/symbols/index'
// ------------------------------------------------------------------
// Builder
// ------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/system/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export namespace TypeSystem {
export function Type<Type, Options = Record<PropertyKey, unknown>>(kind: string, check: (options: Options, value: unknown) => boolean) {
if (Types.TypeRegistry.Has(kind)) throw new TypeSystemDuplicateTypeKind(kind)
Types.TypeRegistry.Set(kind, check)
return (options: Partial<Options> = {}) => Types.Type.Unsafe<Type>({ ...options, [Types.Symbols.Kind]: kind })
return (options: Partial<Options> = {}) => Types.Type.Unsafe<Type>({ ...options, [Types.Kind]: kind })
}
/** Creates a new string format */
export function Format<F extends string>(format: F, check: (value: string) => boolean): F {
Expand Down Expand Up @@ -252,7 +252,7 @@ export function DefaultErrorFunction(schema: Types.TSchema, errorType: ValueErro
case ValueErrorType.Void:
return 'Expected void'
case ValueErrorType.Kind:
return `Expected kind '${schema[Types.Symbols.Kind]}'`
return `Expected kind '${schema[Types.Kind]}'`
default:
return 'Unknown error type'
}
Expand Down
6 changes: 3 additions & 3 deletions src/type/any/any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema, SchemaOptions } from '../schema/index'
import { Symbols } from '../symbols/index'
import { Kind } from '../symbols/index'

// ------------------------------------------------------------------
// TAny
// ------------------------------------------------------------------
export interface TAny extends TSchema {
[Symbols.Kind]: 'Any'
[Kind]: 'Any'
static: any
}

/** `[Json]` Creates an Any type */
export function Any(options: SchemaOptions = {}): TAny {
return { ...options, [Symbols.Kind]: 'Any' } as unknown as TAny
return { ...options, [Kind]: 'Any' } as unknown as TAny
}
6 changes: 3 additions & 3 deletions src/type/array/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ THE SOFTWARE.

import type { TSchema, SchemaOptions } from '../schema/index'
import type { Static } from '../static/index'
import { Symbols } from '../symbols/index'
import { Kind } from '../symbols/index'
import { CloneType } from '../clone/type'

// ------------------------------------------------------------------
Expand All @@ -49,7 +49,7 @@ export interface ArrayOptions extends SchemaOptions {
maxContains?: number
}
export interface TArray<T extends TSchema = TSchema> extends TSchema, ArrayOptions {
[Symbols.Kind]: 'Array'
[Kind]: 'Array'
static: Static<T, this['params']>[]
type: 'array'
items: T
Expand All @@ -58,7 +58,7 @@ export interface TArray<T extends TSchema = TSchema> extends TSchema, ArrayOptio
export function Array<T extends TSchema>(schema: T, options: ArrayOptions = {}): TArray<T> {
return {
...options,
[Symbols.Kind]: 'Array',
[Kind]: 'Array',
type: 'array',
items: CloneType(schema),
} as unknown as TArray<T>
Expand Down
6 changes: 3 additions & 3 deletions src/type/async-iterator/async-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ THE SOFTWARE.

import type { TSchema, SchemaOptions } from '../schema/index'
import type { Static } from '../static/index'
import { Symbols } from '../symbols/index'
import { Kind } from '../symbols/index'
import { CloneType } from '../clone/type'

// ------------------------------------------------------------------
// TAsyncIterator
// ------------------------------------------------------------------
export interface TAsyncIterator<T extends TSchema = TSchema> extends TSchema {
[Symbols.Kind]: 'AsyncIterator'
[Kind]: 'AsyncIterator'
static: AsyncIterableIterator<Static<T, this['params']>>
type: 'AsyncIterator'
items: T
Expand All @@ -44,7 +44,7 @@ export interface TAsyncIterator<T extends TSchema = TSchema> extends TSchema {
export function AsyncIterator<T extends TSchema>(items: T, options: SchemaOptions = {}): TAsyncIterator<T> {
return {
...options,
[Symbols.Kind]: 'AsyncIterator',
[Kind]: 'AsyncIterator',
type: 'AsyncIterator',
items: CloneType(items),
} as unknown as TAsyncIterator<T>
Expand Down
6 changes: 3 additions & 3 deletions src/type/bigint/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema, SchemaOptions } from '../schema/index'
import { Symbols } from '../symbols/index'
import { Kind } from '../symbols/index'

// ------------------------------------------------------------------
// TBigInt
Expand All @@ -40,15 +40,15 @@ export interface BigIntOptions extends SchemaOptions {
multipleOf?: bigint
}
export interface TBigInt extends TSchema, BigIntOptions {
[Symbols.Kind]: 'BigInt'
[Kind]: 'BigInt'
static: bigint
type: 'bigint'
}
/** `[JavaScript]` Creates a BigInt type */
export function BigInt(options: BigIntOptions = {}): TBigInt {
return {
...options,
[Symbols.Kind]: 'BigInt',
[Kind]: 'BigInt',
type: 'bigint',
} as TBigInt
}
6 changes: 3 additions & 3 deletions src/type/boolean/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema, SchemaOptions } from '../schema/index'
import { Symbols } from '../symbols/index'
import { Kind } from '../symbols/index'

// ------------------------------------------------------------------
// TBoolean
// ------------------------------------------------------------------
export interface TBoolean extends TSchema {
[Symbols.Kind]: 'Boolean'
[Kind]: 'Boolean'
static: boolean
type: 'boolean'
}
/** `[Json]` Creates a Boolean type */
export function Boolean(options: SchemaOptions = {}): TBoolean {
return {
...options,
[Symbols.Kind]: 'Boolean',
[Kind]: 'Boolean',
type: 'boolean',
} as unknown as TBoolean
}
6 changes: 3 additions & 3 deletions src/type/constructor/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ THE SOFTWARE.
import type { TSchema, SchemaOptions } from '../schema/index'
import type { Static } from '../static/index'
import type { Ensure } from '../helpers/index'
import { Symbols } from '../symbols/index'
import { Kind } from '../symbols/index'
import { CloneType, CloneRest } from '../clone/type'

// ------------------------------------------------------------------
Expand All @@ -49,7 +49,7 @@ export type TConstructorResolve<T extends TSchema[], U extends TSchema, P extend
// TConstructor
// ------------------------------------------------------------------
export interface TConstructor<T extends TSchema[] = TSchema[], U extends TSchema = TSchema> extends TSchema {
[Symbols.Kind]: 'Constructor'
[Kind]: 'Constructor'
static: TConstructorResolve<T, U, this['params']>
type: 'Constructor'
parameters: T
Expand All @@ -59,7 +59,7 @@ export interface TConstructor<T extends TSchema[] = TSchema[], U extends TSchema
export function Constructor<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TConstructor<T, U> {
return {
...options,
[Symbols.Kind]: 'Constructor',
[Kind]: 'Constructor',
type: 'Constructor',
parameters: CloneRest(parameters),
returns: CloneType(returns),
Expand Down
6 changes: 3 additions & 3 deletions src/type/date/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema, SchemaOptions } from '../schema/index'
import { Symbols } from '../symbols/index'
import { Kind } from '../symbols/index'

// ------------------------------------------------------------------
// TDate
Expand All @@ -45,15 +45,15 @@ export interface DateOptions extends SchemaOptions {
multipleOfTimestamp?: number
}
export interface TDate extends TSchema, DateOptions {
[Symbols.Kind]: 'Date'
[Kind]: 'Date'
static: Date
type: 'date'
}
/** `[JavaScript]` Creates a Date type */
export function Date(options: DateOptions = {}): TDate {
return {
...options,
[Symbols.Kind]: 'Date',
[Kind]: 'Date',
type: 'Date',
} as unknown as TDate
}
8 changes: 4 additions & 4 deletions src/type/enum/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ THE SOFTWARE.

import type { TSchema, SchemaOptions } from '../schema/index'
import { type TLiteral, Literal } from '../literal/index'
import { Symbols } from '../symbols/index'
import { Kind, Hint } from '../symbols/index'
import { Union } from '../union/index'
import { IsUndefined } from '../guard/value'

Expand All @@ -39,8 +39,8 @@ export type TEnumRecord = Record<TEnumKey, TEnumValue>
export type TEnumValue = string | number
export type TEnumKey = string
export interface TEnum<T extends Record<string, string | number> = Record<string, string | number>> extends TSchema {
[Symbols.Kind]: 'Union'
[Symbols.Hint]: 'Enum'
[Kind]: 'Union'
[Hint]: 'Enum'
static: T[keyof T]
anyOf: TLiteral<T[keyof T]>[]
}
Expand All @@ -52,5 +52,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, [Symbols.Hint]: 'Enum' }) as unknown as TEnum<T>
return Union(anyOf, { ...options, [Hint]: 'Enum' }) as unknown as TEnum<T>
}
Loading

0 comments on commit dffcb5f

Please sign in to comment.