Skip to content

Commit

Permalink
Revision 0.30.0 Compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Jul 28, 2023
1 parent 81ab941 commit 169aa5a
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 174 deletions.
84 changes: 50 additions & 34 deletions changelog/0.30.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ This revision includes breaking representation changes to Extended Types and Mod

- Changes
- [Extended Type Representations](#Extended-Type-Representations)
- [TypeScript Code Generation](#TypeScript-Code-Generation)
- [Optional and Readonly](#Optional-and-Readonly)
- [Iterator and AsyncIterator](#Iterator-and-AsyncIterator)
- [Order Independent References](#Order-Independent-References)
- [Value Submodules](#Value-Submodules)
- [Additional Array Constaints](#Additional-Array-Constaints)
- [Reducing Package Size](#Reducing-Package-Size)
- [Reduced Package Size](#Reduced-Package-Size)
- Breaking
- [RegEx Renamed To RegExp](#RegEx-Renamed-To-RegExp)
- [Modifier Symbol Deprecated](#Modifier-Deprecated)

- [RegEx Renamed To RegExp](#RegEx-Renamed-To-RegExp)
- [ValueErrorType Custom Renamed To Kind](#ValueErrorType-Custom-Renamed-To-Kind)

<a name="Extended-Type-Representations"></a>

Expand All @@ -45,6 +46,20 @@ const T = Type.Date() // const T: TDate = { type: '
const U = Type.Undefined() // const U: TUndefined = { type: 'undefined' }
```

<a name="TypeScript Code Generation"></a>

## TypeScript Code Generation

Revision 0.30.0 adds TypeScript code generation support to the TypeCompiler. By specifying a language option on the `.Code()` function, TypeBox will include TS type annotations to the compiled output. This functionality can be used to generate typed TS functions for projects that preference AOT compilation.

```typescript
const Code = TypeCompiler.Code(Type.String(), [], { // return function check(value: any): boolean {
language: 'typescript' // return (
}) // (typeof value === 'string')
// )
// }
```

<a name="Optional-and-Readonly"></a>

## Optional and Readonly
Expand Down Expand Up @@ -190,6 +205,38 @@ Value.Check(T, [1, 1, 1, 1, 1, 1]) // false - more than 5 instan
Value.Check(T, [0]) // false - no instances of 1
```

<a name="Reduced-Package-Size"></a>

## Reduced Package Size

Revision 0.30.0 carries out several internal refactorings in an attempt to reduce package size. This is largely an ongoing effort, with initial work focused on reducing the value and type compositor modules.

```typescript
// Revision 0.29.0
//
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ CompiledMinifiedCompression
├──────────────────────┼────────────┼────────────┼─────────────┤
typebox/compiler'130.3 kb'' 58.2 kb''2.24 x'
typebox/errors'113.3 kb'' 49.8 kb''2.27 x'
typebox/system' 78.8 kb'' 32.2 kb''2.45 x'
typebox/value'180.0 kb'' 77.7 kb''2.32 x'
typebox' 77.7 kb'' 31.7 kb''2.45 x'
└──────────────────────┴────────────┴────────────┴─────────────┘

// Revision 0.30.0
//
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ CompiledMinifiedCompression
├──────────────────────┼────────────┼────────────┼─────────────┤
typebox/compiler'128.4 kb'' 58.1 kb''2.21 x'
typebox/errors'110.7 kb'' 49.6 kb''2.23 x'
typebox/system' 75.5 kb'' 31.2 kb''2.42 x'
typebox/value'179.9 kb'' 78.8 kb''2.28 x'
typebox' 74.4 kb'' 30.7 kb''2.42 x'
└──────────────────────┴────────────┴────────────┴─────────────┘
```

<a name="RegEx-Renamed-To-RegExp"></a>

## RegEx Renamed To RegExp
Expand Down Expand Up @@ -223,34 +270,3 @@ const T = Type.String({ format: 'emoji' })
Value.Check(T, '♥️♦️♠️♣️') // Ok
```
For information on configuring custom formats on Ajv, refer to https://ajv.js.org/guide/formats.html#user-defined-formats


<a name="Reducing-Package-Size"></a>

## Reducing Package Size

```typescript
// Revision 0.29.0
//
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ CompiledMinifiedCompression
├──────────────────────┼────────────┼────────────┼─────────────┤
typebox/compiler'130.3 kb'' 58.2 kb''2.24 x'
typebox/errors'113.3 kb'' 49.8 kb''2.27 x'
typebox/system' 78.8 kb'' 32.2 kb''2.45 x'
typebox/value'180.0 kb'' 77.7 kb''2.32 x'
typebox' 77.7 kb'' 31.7 kb''2.45 x'
└──────────────────────┴────────────┴────────────┴─────────────┘

// Revision 0.30.0
//
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ CompiledMinifiedCompression
├──────────────────────┼────────────┼────────────┼─────────────┤
typebox/compiler'128.9 kb'' 58.5 kb''2.20 x'
typebox/errors'111.1 kb'' 49.8 kb''2.23 x'
typebox/system' 75.9 kb'' 31.4 kb''2.42 x'
typebox/value'180.4 kb'' 79.0 kb''2.28 x'
typebox' 74.8 kb'' 31.0 kb''2.42 x'
└──────────────────────┴────────────┴────────────┴─────────────┘
```
31 changes: 24 additions & 7 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import { TypeSystem } from '@sinclair/typebox/system'
import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Value, ValuePointer } from '@sinclair/typebox/value'
import { Type, TypeGuard, Kind, Static, TSchema } from '@sinclair/typebox'
import { Type, TypeGuard, Kind, Static, TSchema, TypeRegistry } from '@sinclair/typebox'

const T = Type.Intersect([Type.String(), Type.Number()])
const F = Type.Unsafe({ })
TypeRegistry.Set('Foo', () => true)
const S = Type.Object(
{
f: F,
q: F,
x: Type.Number(),
y: Type.Number(),
z: Type.Number({ $id: 'Z' }),
},
{ $id: 'S' },
)

const R = Type.RegExp('hello world')
const T = Type.Object(
{
a: S,
b: S,
},
{ $id: 'T' },
)

const M = Type.Object({
x: Type.Number(),
y: Type.String(),
z: Type.Boolean(),
const C = TypeCompiler.Code(T, [], {
language: 'typescript',
})

console.log(C)
Loading

0 comments on commit 169aa5a

Please sign in to comment.