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 Dec 2, 2023
1 parent 573cdb1 commit ecc71e8
Show file tree
Hide file tree
Showing 396 changed files with 18,727 additions and 6,877 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
node: [16.x, 18.x, 20.x]
os: [ubuntu-latest, windows-latest, macOS-latest]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install Node
uses: actions/setup-node@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
node: [20.x]
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install Node
uses: actions/setup-node@v3
with:
Expand Down
5 changes: 0 additions & 5 deletions benchmark/measurement/index.ts

This file was deleted.

97 changes: 97 additions & 0 deletions changelog/0.32.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
## [0.32.0](https://www.npmjs.com/package/@sinclair/typebox/v/0.32.0)

## Overview

Revision 0.32.0 is milestone revision. This revision carries out a complete modularization of `Type.*` and `Value.*`, as well as remodelling the TypeBox API to support selectively importing individual types. In addition to project restructuring, this revision also includes two new Types (`Const` and `Deref`) two new Value functions (`Clean` and `Default`) as well as many additional optimizations to type inference.

This is a significant revision for TypeBox. The updates in this revision should not break most users, however, many internal inference types that were previously exported on `typebox.ts` have been made private within module. Testing has been carried out to ensure the documented frontend for TypeBox is retained, however due to internal inference types being made fully private. Some users may experience some breakage.

A minor revision is required.

## Contents

- Enhancements
- [Modularization](#Modularization)
- [Types](#Modularization-Types)
- [Values](#Modularization-Values)
- [Bundle Size](#Modularization-Bundle-Size)
- [Types](#Types)
- [Const](#Const)
- [Deref](#Deref)
- [Values](#Values)
- [Clean](#Clean)
- [Default](#Default)
- Deprecations
- [RegExp](#RegExp)
- [TypeSystem Format](#TypeSystemFormat)
- [TypeSystem Type](#TypeSystemType)
- Breaking
- [JsonTypeBuilder and JavaScriptTypeBuilder](#JsonTypeBuilder-and-JavaScriptTypeBuilder)
- [TypeSystemPolicy](#TypeSystemPolicy)

## Modularization

Revision 0.32.0 carries out a complete restructuring of TypeBox's internals to modularize the type system. These updates enable for the following.

- Enable bundlers to properly tree-shake TypeBox's type system.
- Enable additional options for integrators re-exporting TypeBox's interface.
- Further optimize bundle sizes for Value and TypeCompiler.
- Improve maintainability of the type system.
- Make nessasarily provisions for ESM publishing.

### Types

Revision 0.32.0 now exposes `Type.*` as a default export as well as enables all types to be selectively imported. When importing selective types, bundlers may be able to tree-shake unused types. The following imports are now supported.

```typescript
import { Type, type Static } from '@sinclair/typebox' // classic
import Type, { type Static } from '@sinclair/typebox' // modern
import { Object, String, Number, ... } from '@sinclair/typebox' // selective
```
### Values
Revision 0.32.0 carries out modularization of `Value.*`. Individual Value functions can not be imported directly without specifying the sub module path.
```typescript
import { Value } from '@sinclair/typebox/value' // classic
import Value from '@sinclair/typebox/value' // modern
import { Check, Clone, Cast } from '@sinclair/typebox/value' // selective
```
### Bundle Size
The modularization in 0.32.0 has enabled significant bundle size optimizations across Value, TypeCompiler, Errors and System sub modules.
Revision 0.31.0
```
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ CompiledMinifiedCompression
├──────────────────────┼────────────┼────────────┼─────────────┤
typebox/compiler'163.6 kb'' 71.6 kb''2.28 x'
typebox/errors'113.3 kb'' 50.1 kb''2.26 x'
typebox/system' 83.9 kb'' 37.5 kb''2.24 x'
typebox/value'191.1 kb'' 82.3 kb''2.32 x'
typebox' 73.8 kb'' 32.3 kb''2.29 x'
└──────────────────────┴────────────┴────────────┴─────────────┘
```

Revision 0.32.0

```
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ Compiled │ Minified │ Compression │
├──────────────────────┼────────────┼────────────┼─────────────┤
│ typebox/compiler │ '109.3 kb' │ ' 48.5 kb' │ '2.25 x' │
│ typebox/errors │ ' 54.8 kb' │ ' 24.9 kb' │ '2.20 x' │
│ typebox/system │ ' 12.1 kb' │ ' 6.1 kb' │ '1.98 x' │
│ typebox/type │ ' 77.8 kb' │ ' 32.8 kb' │ '2.37 x' │
│ typebox/value │ '143.7 kb' │ ' 61.2 kb' │ '2.35 x' │
│ typebox │ ' 77.9 kb' │ ' 32.8 kb' │ '2.37 x' │
└──────────────────────┴────────────┴────────────┴─────────────┘
```
148 changes: 148 additions & 0 deletions example/annotation/annotation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*--------------------------------------------------------------------------
@sinclair/typebox
The MIT License (MIT)
Copyright (c) 2017-2023 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 * as Types from '@sinclair/typebox'

// -------------------------------------------------------------------
// Annotation
//
// Generates TypeScript Type Annotations from TypeBox types
// -------------------------------------------------------------------
/** Generates TypeScript Type Annotations from TypeBox types */
export namespace Annotation {
// -----------------------------------------------------------------
// Escape
// -----------------------------------------------------------------
function Escape(content: string) {
return content.replace(/'/g, "\\'")
}
// -----------------------------------------------------------------
// Types
// -----------------------------------------------------------------
function Intersect(schema: Types.TSchema[], references: Types.TSchema[]): string {
const [L, ...R] = schema
// prettier-ignore
return R.length === 0
? `${Visit(L, references)}`
: `${Visit(L, references)} & ${Intersect(R, references)}`
}
function Union(schema: Types.TSchema[], references: Types.TSchema[]): string {
const [L, ...R] = schema
// prettier-ignore
return R.length === 0
? `${Visit(L, references)}`
: `${Visit(L, references)} | ${Union(R, references)}`
}
function Tuple(schema: Types.TSchema[], references: Types.TSchema[]): string {
const [L, ...R] = schema
// prettier-ignore
return R.length > 0
? `${Visit(L, references)}, ${Tuple(R, references)}`
: ``
}
function Property(schema: Types.TProperties, K: string, references: Types.TSchema[]): string {
const TK = schema[K]
// prettier-ignore
return (
Types.TypeGuard.TOptional(TK) && Types.TypeGuard.TReadonly(TK) ? `readonly ${K}?: ${Visit(TK, references)}` :
Types.TypeGuard.TReadonly(TK) ? `readonly ${K}: ${Visit(TK, references)}` :
Types.TypeGuard.TOptional(TK) ? `${K}?: ${Visit(TK, references)}` :
`${K}: ${Visit(TK, references)}`
)
}
function Properties(schema: Types.TProperties, K: string[], references: Types.TSchema[]): string {
const [L, ...R] = K
// prettier-ignore
return R.length === 0
? `${Property(schema, L, references)}`
: `${Property(schema, L, references)}; ${Properties(schema, R, references)}`
}
function Parameters(schema: Types.TSchema[], I: number, references: Types.TSchema[]): string {
const [L, ...R] = schema
// prettier-ignore
return R.length === 0
? `param_${I}: ${Visit(L, references)}`
: `param_${I}: ${Visit(L, references)}, ${Parameters(R, I + 1, references)}`
}
function Literal(schema: Types.TLiteral, references: Types.TSchema[]): string {
return typeof schema.const === 'string' ? `'${Escape(schema.const)}'` : schema.const.toString()
}
function Record(schema: Types.TRecord, references: Types.TSchema[]): string {
// prettier-ignore
return (
Types.PatternBooleanExact in schema.patternProperties ? `Record<boolean, ${Visit(schema.patternProperties[Types.PatternBooleanExact], references)}>` :
Types.PatternNumberExact in schema.patternProperties ? `Record<number, ${Visit(schema.patternProperties[Types.PatternNumberExact], references)}>` :
Types.PatternStringExact in schema.patternProperties ? `Record<string, ${Visit(schema.patternProperties[Types.PatternStringExact], references)}>` :
`{}`
)
}
function TemplateLiteral(schema: Types.TTemplateLiteral, references: Types.TSchema[]) {
const E = Types.TemplateLiteralParseExact(schema.pattern)
if (!Types.IsTemplateLiteralFinite(E)) return 'string'
return [...Types.TemplateLiteralGenerate(E)].map((literal) => `'${Escape(literal)}'`).join(' | ')
}
function Visit(schema: Types.TSchema, references: Types.TSchema[]): string {
// prettier-ignore
return (
Types.TypeGuard.TAny(schema) ? 'any' :
Types.TypeGuard.TArray(schema) ? `${Visit(schema.items, references)}[]` :
Types.TypeGuard.TAsyncIterator(schema) ? `AsyncIterableIterator<${Visit(schema.items, references)}>` :
Types.TypeGuard.TBigInt(schema) ? `bigint` :
Types.TypeGuard.TBoolean(schema) ? `boolean` :
Types.TypeGuard.TConstructor(schema) ? `new (${Parameters(schema.parameter, 0, references)}) => ${Visit(schema.returns, references)}` :
Types.TypeGuard.TDate(schema) ? 'Date' :
Types.TypeGuard.TFunction(schema) ? `(${Parameters(schema.parameters, 0, references)}) => ${Visit(schema.returns, references)}` :
Types.TypeGuard.TInteger(schema) ? 'number' :
Types.TypeGuard.TIntersect(schema) ? `(${Intersect(schema.allOf, references)})` :
Types.TypeGuard.TIterator(schema) ? `IterableIterator<${Visit(schema.items, references)}>` :
Types.TypeGuard.TLiteral(schema) ? `${Literal(schema, references)}` :
Types.TypeGuard.TNever(schema) ? `never` :
Types.TypeGuard.TNull(schema) ? `null` :
Types.TypeGuard.TNot(schema) ? 'unknown' :
Types.TypeGuard.TNumber(schema) ? 'number' :
Types.TypeGuard.TObject(schema) ? `{ ${Properties(schema.properties, Object.getOwnPropertyNames(schema.properties), references)} }` :
Types.TypeGuard.TPromise(schema) ? `Promise<${Visit(schema.item, references)}>` :
Types.TypeGuard.TRecord(schema) ? `${Record(schema, references)}` :
Types.TypeGuard.TRef(schema) ? `${Visit(Types.Type.Deref(schema, references), references)}` :
Types.TypeGuard.TString(schema) ? 'string' :
Types.TypeGuard.TSymbol(schema) ? 'symbol' :
Types.TypeGuard.TTemplateLiteral(schema) ? `${TemplateLiteral(schema, references)}` :
Types.TypeGuard.TThis(schema) ? 'unknown' : // requires named interface
Types.TypeGuard.TTuple(schema) ? `[${Tuple(schema.items || [], references)}]` :
Types.TypeGuard.TUint8Array(schema) ? `Uint8Array` :
Types.TypeGuard.TUndefined(schema) ? 'undefined' :
Types.TypeGuard.TUnion(schema) ? `${Union(schema.anyOf, references)}` :
Types.TypeGuard.TVoid(schema) ? `void` :
'unknown'
)
}
/** Generates a TypeScript annotation for the given schema */
export function Code(schema: Types.TSchema, references: Types.TSchema[] = []): string {
return Visit(schema, references)
}
}
1 change: 1 addition & 0 deletions example/annotation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './annotation'
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
File renamed without changes.
6 changes: 3 additions & 3 deletions examples/collections/map.ts → example/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
File renamed without changes.
4 changes: 2 additions & 2 deletions examples/collections/set.ts → example/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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit ecc71e8

Please sign in to comment.