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 6, 2023
1 parent 573cdb1 commit e77faea
Show file tree
Hide file tree
Showing 421 changed files with 22,222 additions and 6,990 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.

357 changes: 357 additions & 0 deletions changelog/0.32.0.md

Large diffs are not rendered by default.

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.
23 changes: 6 additions & 17 deletions examples/typedef/typedef.ts → example/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/index.mjs'

// --------------------------------------------------------------------------
// 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
Loading

0 comments on commit e77faea

Please sign in to comment.