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 3ef8d3a commit 4e85569
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 41 deletions.
76 changes: 40 additions & 36 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
import Type from '@sinclair/typebox'
import Type, { type Static } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'
import { TypeCompiler } from '@sinclair/typebox/compiler'

const X = TypeCompiler.Code(
Type.Object({
x: Type.String(),
y: Type.BigInt(),
z: Type.Array(Type.Null()),
}),
)

console.log(X)

const A = Type.Transform(Type.String())
.Decode((value: string): Date => new Date(value))
.Encode((value: Date): string => value.toISOString())

const B = Type.Union([Type.Object({ date: A }), Type.Number()])

const C = Type.Transform(B)
.Decode((value) => {
console.log(value)
if (typeof value === 'object') {
// date is supposed to be a Date according to type inference, but it's a string when it's logged.
console.log('union', typeof value.date) // string
}
return value
})
.Encode((o) => o)

const D = Type.Transform(Type.Object({ date: A }))
.Decode((o) => {
console.log('simple', typeof o.date) // object
return o
})
.Encode((o) => o)

const R1 = Value.Decode(C, [], { date: new Date().toISOString() })
const A = Type.Number({})

// console.log(AA)

// const X = TypeCompiler.Code(
// Type.Object({
// x: Type.String(),
// y: Type.BigInt(),
// z: Type.Array(Type.Null()),
// }),
// )

// console.log(X)

// const A = Type.Transform(Type.String())
// .Decode((value: string): Date => new Date(value))
// .Encode((value: Date): string => value.toISOString())

// const B = Type.Union([Type.Object({ date: A }), Type.Number()])

// const C = Type.Transform(B)
// .Decode((value) => {
// console.log(value)
// if (typeof value === 'object') {
// // date is supposed to be a Date according to type inference, but it's a string when it's logged.
// console.log('union', typeof value.date) // string
// }
// return value
// })
// .Encode((o) => o)

// const D = Type.Transform(Type.Object({ date: A }))
// .Decode((o) => {
// console.log('simple', typeof o.date) // object
// return o
// })
// .Encode((o) => o)

// const R1 = Value.Decode(C, [], { date: new Date().toISOString() })
26 changes: 21 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ $ npm install @sinclair/typebox --save
## Example

```typescript
import Type { 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 @@ -121,7 +121,7 @@ License MIT
The following shows general usage.
```typescript
import Type { type Static } from '@sinclair/typebox'
import { Type, type Static } from '@sinclair/typebox'

//--------------------------------------------------------------------------------------------
//
Expand Down Expand Up @@ -291,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 @@ -691,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, { type Static, type 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 @@ -1041,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, { TypeGuard } from '@sinclair/typebox'
import { Type, TypeGuard } from '@sinclair/typebox'

const A: unknown = { ... }

Expand Down Expand Up @@ -1349,7 +1365,7 @@ $ npm install ajv ajv-formats --save
```
```typescript
import Type from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import addFormats from 'ajv-formats'
import Ajv from 'ajv'

Expand Down

0 comments on commit 4e85569

Please sign in to comment.