Skip to content

Commit

Permalink
Revision 0.28.3 (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 authored Apr 21, 2023
1 parent f8e5028 commit 749b453
Show file tree
Hide file tree
Showing 201 changed files with 2,426 additions and 2,231 deletions.
57 changes: 57 additions & 0 deletions changelog/0.28.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## [0.28.3](https://www.npmjs.com/package/@sinclair/typebox/v/0.28.3)

## Overview

Revision 0.28.3 adds a new Rest type to support variadic type composition.

## Contents

- Enhancements
- [Variadic Types](#Variadic-Types)

<a href="Variadic-Types"></a>

## Variadic Types

Revision 0.28.3 adds a new type named `Type.Rest`. This type is used to extract a tuple array of type of `[...TSchema]`. The return value of this type is not strictly JSON Schema, however the tuple array can be used as a parameter to other types that accept tuples as their arguments.

### Tuple Concatenation

```typescript
// TypeScript

type A = [1, 2]

type B = [3, 4]

type C = [...A, ...B]

// TypeBox

const A = Type.Tuple([Type.Literal(1), Type.Literal(2)])

const B = Type.Tuple([Type.Literal(3), Type.Literal(4)])

const C = Type.Tuple([...Type.Rest(A), ...Type.Rest(B)])
```

### Tuple To Parameter

```typescript
// TypeScript

type P = [number, number]

type F1 = (param: [...P]) => void

type F2 = (param: [...P, number]) => void

// TypeBox

const P = Type.Tuple([Type.Number(), Type.Number()])

const F1 = Type.Function(Type.Rest(P), Type.Void())

const F2 = Type.Function([...Type.Rest(P), Type.Number()], Type.Void())
```

2 changes: 1 addition & 1 deletion example/template-dsl/template-dsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export type TTemplateLiteralParser<T extends string> = Ensure<TTemplateLiteral<A
// ---------------------------------------------------------------------
namespace TemplateLiteralParser {
export function * ParseUnion(template: string): IterableIterator<TTemplateLiteralKind> {
const trim = template.trim()
const trim = template.trim().replace(/"|'/g, '')
if(trim === 'boolean') return yield Type.Boolean()
if(trim === 'number') return yield Type.Number()
if(trim === 'bigint') return yield Type.BigInt()
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.28.2",
"version": "0.28.3",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
72 changes: 56 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ License MIT
- [Conditional](#types-conditional)
- [Template](#types-template-literal)
- [Indexed](#types-indexed)
- [Variadic](#types-variadic)
- [Guards](#types-guards)
- [Unsafe](#types-unsafe)
- [Strict](#types-strict)
Expand Down Expand Up @@ -256,7 +257,8 @@ The following table lists the Standard TypeBox types. These types are fully comp
│ }) │ } │ properties: { │
│ │ │ x: { │
│ │ │ type: 'number'
│ │ │ }, { │
│ │ │ }, │
│ │ │ y: { │
│ │ │ type: 'number'
│ │ │ } │
│ │ │ } │
Expand All @@ -277,14 +279,6 @@ The following table lists the Standard TypeBox types. These types are fully comp
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
const T = Type.Index( │ type T = { │ const T = { │
│ Type.Object({ │ x: number, │ type: number
x: Type.Number(), │ y: string │ } │
│ y: Type.String() │ }['x'] │ │
│ }), ['x'] │ │ │
│ ) │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
enum Foo { │ enum Foo { │ const T = { │
A, │ A, │ anyOf: [{ │
│ B │ B │ type: 'number', │
Expand Down Expand Up @@ -468,6 +462,28 @@ The following table lists the Standard TypeBox types. These types are fully comp
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
const T = Type.Index( │ type T = { │ const T = { │
│ Type.Object({ │ x: number, │ type: 'number'
x: Type.Number(), │ y: string │ } │
│ y: Type.String() │ }['x'] │ │
│ }), ['x'] │ │ │
│ ) │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
const A = Type.Tuple([ │ type A = [0, 1] │ const T = { │
│ Type.Literal(0), │ type B = [2, 3] │ type: 'array', │
│ Type.Literal(1) │ type T = [...A, ...B] │ items: [ │
│ ]) │ │ { const: 0 }, │
│ const B = Type.Tuple([ │ │ { const: 1 }, │
| Type.Literal(2), │ │ { const: 2 }, │
| Type.Literal(3) │ │ { const: 3 } │
│ ]) │ │ ], │
│ const T = Type.Tuple([ │ │ additionalItems: false, │
| ...Type.Rest(A), │ │ minItems: 4, │
| ...Type.Test(B) │ │ maxItems: 4
│ ]) │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
const T = Type.Object({ │ type T = { │ const R = { │
│ x: Type.Number(), │ x: number, │ $ref: 'T'
y: Type.Number() │ y: number │ } │
Expand Down Expand Up @@ -856,10 +872,34 @@ const A = Type.Index(T, ['x']) // type A = T['x']

const B = Type.Index(T, Type.KeyOf(T)) // type B = T[keyof T]
```
<a name='types-variadic'></a>
### Variadic Types
Variadic types are supported with `Type.Rest`. This type will extract interior types from a tuple and return them as a flat array. This array can then be passed to other types that accept arrays as arguments.
The following creates variadic functions using the Rest type.
```typescript
// TypeScript

type P = [number, number]

type F1 = (param: [...P]) => void

type F2 = (param: [...P, number]) => void

// TypeBox

const P = Type.Tuple([Type.Number(), Type.Number()])

const F1 = Type.Function(Type.Rest(P), Type.Void())

const F2 = Type.Function([...Type.Rest(P), Type.Number()], Type.Void())
```
<a name='types-unsafe'></a>
### Unsafe
### Unsafe Types
Use `Type.Unsafe` to create custom schematics with user defined inference rules.
Expand Down Expand Up @@ -904,7 +944,7 @@ type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
<a name='types-guards'></a>
### Guards
### Type Gaurds
TypeBox provides a `TypeGuard` module that can be used for reflection and asserting values as types.
Expand Down Expand Up @@ -1436,11 +1476,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ CompiledMinifiedCompression
├──────────────────────┼────────────┼────────────┼─────────────┤
typebox/compiler'127.2 kb'' 56.9 kb''2.23 x'
typebox/errors'110.9 kb'' 49.2 kb''2.25 x'
typebox/system' 76.4 kb'' 31.5 kb''2.42 x'
typebox/value'176.9 kb'' 76.8 kb''2.30 x'
typebox' 75.3 kb'' 31.1 kb''2.42 x'
typebox/compiler'126.7 kb'' 56.6 kb''2.24 x'
typebox/errors'110.4 kb'' 48.8 kb''2.26 x'
typebox/system' 75.9 kb'' 31.1 kb''2.44 x'
typebox/value'176.4 kb'' 76.4 kb''2.31 x'
typebox' 74.8 kb'' 30.7 kb''2.44 x'
└──────────────────────┴────────────┴────────────┴─────────────┘
```
Expand Down
Loading

0 comments on commit 749b453

Please sign in to comment.