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 29, 2023
1 parent 573cdb1 commit 249ec87
Show file tree
Hide file tree
Showing 263 changed files with 14,486 additions and 6,589 deletions.
3 changes: 3 additions & 0 deletions benchmark/compression/module/typebox-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Type } from '@sinclair/typebox'

const T = Type.String()
2 changes: 1 addition & 1 deletion benchmark/compression/module/typebox.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Type } from '@sinclair/typebox'
import Type from '@sinclair/typebox'

const T = Type.String()
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.
6 changes: 3 additions & 3 deletions examples/collections/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TypeCheck, TypeCompiler, ValueError } from '@sinclair/typebox/compiler'
import { TSchema, Static, TypeBoxError } from '@sinclair/typebox'
import { TSchema, Static } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

// ----------------------------------------------------------------
// TypeArrayError
// ----------------------------------------------------------------
export class TypeArrayError extends TypeBoxError {
export class TypeArrayError extends Error {
constructor(message: string) {
super(`${message}`)
}
}
export class TypeArrayLengthError extends TypeBoxError {
export class TypeArrayLengthError extends Error {
constructor() {
super('arrayLength not a number')
}
Expand Down
6 changes: 3 additions & 3 deletions examples/collections/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TypeCheck, TypeCompiler, ValueError } from '@sinclair/typebox/compiler'
import { TSchema, Static, TypeBoxError } from '@sinclair/typebox'
import { TSchema, Static } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

// ----------------------------------------------------------------
// TypeMapKeyError
// ----------------------------------------------------------------
export class TypeMapKeyError extends TypeBoxError {
export class TypeMapKeyError extends Error {
constructor(message: string) {
super(`${message} for key`)
}
}
export class TypeMapValueError extends TypeBoxError {
export class TypeMapValueError extends Error {
constructor(key: unknown, message: string) {
super(`${message} for key ${JSON.stringify(key)}`)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/collections/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TypeCheck, TypeCompiler, ValueError } from '@sinclair/typebox/compiler'
import { TSchema, Static, TypeBoxError } from '@sinclair/typebox'
import { TSchema, Static } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

// ----------------------------------------------------------------
// Errors
// ----------------------------------------------------------------
export class TypeSetError extends TypeBoxError {
export class TypeSetError extends Error {
constructor(message: string) {
super(`${message}`)
}
Expand Down
3 changes: 1 addition & 2 deletions examples/prototypes/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import {
TTuple,
TProperties,
TIntersect,
IntersectType,
TUnion,
TNever
} from '@sinclair/typebox'
Expand Down Expand Up @@ -75,7 +74,7 @@ export type TEvaluateArray<T extends TSchema[]> = T extends [infer L, ...infer
[]
// prettier-ignore
export type TEvaluate<T extends TSchema> =
T extends TIntersect<infer S> ? IntersectType<TEvaluateIntersectRest<S>> :
T extends TIntersect<infer S> ? TIntersect<TEvaluateIntersectRest<S>> :
T extends TUnion<infer S> ? TUnion<TEvaluateArray<S>> :
T extends TConstructor<infer P, infer R> ? TConstructor<TEvaluateArray<P>, TEvaluate<R>> :
T extends TFunction<infer P, infer R> ? TFunction<TEvaluateArray<P>, TEvaluate<R>> :
Expand Down
1 change: 0 additions & 1 deletion examples/prototypes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

export * from './const'
export * from './evaluate'
export * from './partial-deep'
export * from './union-enum'
Expand Down
9 changes: 5 additions & 4 deletions examples/prototypes/partial-deep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TypeGuard, Type, TSchema, TIntersect, TUnion, TObject, TPartial, TProperties, AssertRest, AssertType, Evaluate } from '@sinclair/typebox'
import { TypeGuard, Type, TSchema, TIntersect, TUnion, TObject, TPartial, TProperties, Evaluate } from '@sinclair/typebox'

// -------------------------------------------------------------------------------------
// TDeepPartial
// -------------------------------------------------------------------------------------
export type TPartialDeepProperties<T extends TProperties> = {
[K in keyof T]: TPartial<T[K]>
}
export type TPartialDeepRest<T extends TSchema[]> = T extends [infer L, ...infer R]
? [TPartial<AssertType<L>>, ...TPartialDeepRest<AssertRest<R>>]
: []
export type TPartialDeepRest<T extends TSchema[]> =
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? [TPartial<L>, ...TPartialDeepRest<R>]
: []
export type TPartialDeep<T extends TSchema> =
T extends TIntersect<infer S> ? TIntersect<TPartialDeepRest<S>> :
T extends TUnion<infer S> ? TUnion<TPartialDeepRest<S>> :
Expand Down
23 changes: 6 additions & 17 deletions examples/typedef/typedef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,10 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TypeSystemErrorFunction, DefaultErrorFunction } from '@sinclair/typebox/system'
import * as Types from '@sinclair/typebox'
import * as Types from '@sinclair/typebox/type'

// --------------------------------------------------------------------------
// Utility Types
// --------------------------------------------------------------------------
export type Assert<T, U> = T extends U ? T : never
export type Base = { m: string, t: string }
export type Base16 = { m: 'F', t: '01', '0': '1', '1': '2', '2': '3', '3': '4', '4': '5', '5': '6', '6': '7', '7': '8', '8': '9', '9': 'A', 'A': 'B', 'B': 'C', 'C': 'D', 'D': 'E', 'E': 'F', 'F': '0' }
export type Base10 = { m: '9', t: '01', '0': '1', '1': '2', '2': '3', '3': '4', '4': '5', '5': '6', '6': '7', '7': '8', '8': '9', '9': '0' }
export type Reverse<T extends string> = T extends `${infer L}${infer R}` ? `${Reverse<R>}${L}` : T
export type Tick<T extends string, B extends Base> = T extends keyof B ? B[T] : never
export type Next<T extends string, B extends Base> = T extends Assert<B, Base>['m'] ? Assert<B, Base>['t'] : T extends `${infer L}${infer R}` ? L extends Assert<B, Base>['m'] ? `${Assert<Tick<L, B>, string>}${Next<R, B>}` : `${Assert<Tick<L, B>, string>}${R}` : never
export type Increment<T extends string, B extends Base = Base10> = Reverse<Next<Reverse<T>, B>>
// --------------------------------------------------------------------------
// SchemaOptions
// Metadata
// --------------------------------------------------------------------------
export interface Metadata {
[name: string]: any
Expand All @@ -65,8 +54,8 @@ export interface TBoolean extends Types.TSchema {
// --------------------------------------------------------------------------
// TUnion
// --------------------------------------------------------------------------
type InferUnion<T extends TStruct[], D extends string, Index = string> = T extends [infer L, ...infer R]
? Types.Evaluate<{ [_ in D]: Index } & Types.Static<Types.AssertType<L>>> | InferUnion<Types.AssertRest<R>, D, Increment<Types.Assert<Index, string>>>
export type InferUnion<T extends TStruct[], D extends string, Index = string> = T extends [infer L, ...infer R]
? Types.Evaluate<{ [_ in D]: Index } & Types.Static<Types.AssertType<L>>> | InferUnion<Types.AssertRest<R>, D, Types.Increment<Types.Assert<Index, string>>>
: never

export interface TUnion<T extends TStruct[] = TStruct[], D extends string = string> extends Types.TSchema {
Expand Down Expand Up @@ -177,7 +166,7 @@ export interface StructMetadata extends Metadata {
}
export interface TStruct<T extends TFields = TFields> extends Types.TSchema, StructMetadata {
[Types.Kind]: 'TypeDef:Struct'
static: Types.PropertiesReduce<T, this['params']>
static: Types.ObjectResolve<T, this['params']>
optionalProperties: { [K in Types.Assert<OptionalKeys<T>, keyof T>]: T[K] }
properties: { [K in Types.Assert<RequiredKeys<T>, keyof T>]: T[K] }
}
Expand Down Expand Up @@ -240,7 +229,7 @@ export namespace TimestampFormat {
// --------------------------------------------------------------------------
// ValueCheck
// --------------------------------------------------------------------------
export class ValueCheckError extends Types.TypeBoxError {
export class ValueCheckError extends Error {
constructor(public readonly schema: Types.TSchema) {
super('Unknown type')
}
Expand Down
8 changes: 8 additions & 0 deletions hammer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { readFileSync } from 'fs'
// Clean
// -------------------------------------------------------------------------------
export async function clean() {
await folder('node_modules/typebox').delete()
await folder('target').delete()
}
// -------------------------------------------------------------------------------
Expand Down Expand Up @@ -65,6 +66,13 @@ export async function build(target = 'target/build') {
await folder(target).add('license')
await shell(`cd ${target} && npm pack`)
}
// -------------------------------------------------------------------------------
// Install
// -------------------------------------------------------------------------------
export async function install_local(target = 'target/typebox') {
await build(target)
await folder('node_modules').add(target)
}
// -------------------------------------------------------------
// Publish
// -------------------------------------------------------------
Expand Down
38 changes: 20 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.31.28",
"version": "0.32.0-dev-1",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand All @@ -15,38 +15,40 @@
"exports": {
"./compiler": "./compiler/index.js",
"./errors": "./errors/index.js",
"./type": "./type/index.js",
"./system": "./system/index.js",
"./value/cast": "./value/cast.js",
"./value/check": "./value/check.js",
"./value/clone": "./value/clone.js",
"./value/convert": "./value/convert.js",
"./value/create": "./value/create.js",
"./value/delta": "./value/delta.js",
"./value/deref": "./value/deref.js",
"./value/equal": "./value/equal.js",
"./value/guard": "./value/guard.js",
"./value/hash": "./value/hash.js",
"./value/mutate": "./value/mutate.js",
"./value/pointer": "./value/pointer.js",
"./value/transform": "./value/transform.js",
"./value/cast": "./value/cast/index.js",
"./value/check": "./value/check/index.js",
"./value/clone": "./value/clone/index.js",
"./value/convert": "./value/convert/index.js",
"./value/create": "./value/create/index.js",
"./value/delta": "./value/delta/index.js",
"./value/deref": "./value/deref/index.js",
"./value/equal": "./value/equal/index.js",
"./value/guard": "./value/guard/index.js",
"./value/hash": "./value/hash/index.js",
"./value/mutate": "./value/mutate/index.js",
"./value/pointer": "./value/pointer/index.js",
"./value/transform": "./value/transform/index.js",
"./value": "./value/index.js",
".": "./typebox.js"
".": "./index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/sinclairzx81/typebox"
},
"scripts": {
"benchmark:compression": "hammer task benchmark_compression",
"benchmark:measurement": "hammer task benchmark_measurement",
"benchmark": "hammer task benchmark",
"install:local": "hammer task install_local",
"test:typescript": "hammer task test_typescript",
"test:static": "hammer task test_static",
"test:runtime": "hammer task test_runtime",
"test": "hammer task test",
"clean": "hammer task clean",
"format": "hammer task format",
"start": "hammer task start",
"benchmark:compression": "hammer task benchmark_compression",
"benchmark:measurement": "hammer task benchmark_measurement",
"benchmark": "hammer task benchmark",
"build": "hammer task build",
"publish": "hammer task publish",
"publish:dev": "hammer task publish_dev"
Expand Down
32 changes: 21 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,10 @@
$ npm install @sinclair/typebox --save
```

#### Esm + Deno

```typescript
import { Type, Static } from 'https://esm.sh/@sinclair/typebox'
```

## Example

```typescript
import { Type, Static } from '@sinclair/typebox'
import { Type, type Static } from '@sinclair/typebox'

const T = Type.Object({ // const T = {
x: Type.Number(), // type: 'object',
Expand Down Expand Up @@ -127,7 +121,7 @@ License MIT
The following shows general usage.
```typescript
import { Type, Static } from '@sinclair/typebox'
import { Type, type Static } from '@sinclair/typebox'

//--------------------------------------------------------------------------------------------
//
Expand Down Expand Up @@ -297,6 +291,22 @@ The following table lists the supported Json types. These types are fully compat
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
const T = Type.Const({ │ type T = { │ const T = { │
│ x: 1, │ readonly x: 1, │ type: 'object', │
│ y: 2, │ readonly y: 2required: ['x', 'y'], │
│ } as const) │ } │ properties: { │
│ │ │ x: { │
│ │ │ type: 'number', │
│ │ │ const: 1
│ │ │ }, │
│ │ │ y: { │
│ │ │ type: 'number', │
│ │ │ const: 2
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
const T = Type.KeyOf( │ type T = keyof { │ const T = { │
│ Type.Object({ │ x: number, │ anyOf: [{ │
│ x: Type.Number(), │ y: numbertype: 'string', │
Expand Down Expand Up @@ -697,7 +707,7 @@ Object properties can be modified with Readonly and Optional. The following tabl
Generic types can be created with generic functions. All types extend the base type TSchema. It is common to constrain generic function arguments to this type. The following creates a generic Vector type.
```typescript
import { Type, Static, TSchema } from '@sinclair/typebox'
import { Type, type Static, type TSchema } from '@sinclair/typebox'

const Vector = <T extends TSchema>(t: T) => Type.Object({ x: t, y: t, z: t })

Expand Down Expand Up @@ -1047,7 +1057,7 @@ type S = Static<typeof T> // type S = 'A' | 'B' | 'C'
TypeBox can type check its own types with the TypeGuard module. This module is written for reflection and provides structural tests for every TypeBox type. Functions of this module return `is` guards which can be used with TypeScript control flow assertions to obtain schema inference. The following guards that the value A is TString.
```typescript
import { Type, Kind, TypeGuard } from '@sinclair/typebox'
import { Type, TypeGuard } from '@sinclair/typebox'

const A: unknown = { ... }

Expand Down Expand Up @@ -1311,7 +1321,7 @@ The TypeBox type system can be extended with additional types and formats using
Use the TypeRegistry to register a new type. The Kind must match the registered type name.
```typescript
import { TypeRegistry, Kind } from '@sinclair/typebox'
import { TypeRegistry, Symbols } from '@sinclair/typebox'

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

Expand Down
Loading

0 comments on commit 249ec87

Please sign in to comment.