Skip to content

Commit

Permalink
Static Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 7, 2023
1 parent 7855086 commit a60d06e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
50 changes: 36 additions & 14 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
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'

// Todo: Investigate Union Default (initialize interior types)
// Todo: Implement Value.Clean() Tests - Done
// -----------------------------------------------------------
// Create: Type
// -----------------------------------------------------------

import { Type, Static } from '@sinclair/typebox';

export const A = Type.Object({
a: Type.ReadonlyOptional(Type.Number()),
b: Type.Readonly(Type.Number()),
c: Type.Optional(Type.Number()),
d: Type.Number()
const T = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
})
const B = Type.Required(A)
const C = Type.Partial(A)

type A = Static<typeof A>
type B = Static<typeof B>
type C = Static<typeof C>
type T = Static<typeof T>

console.log(T)

// -----------------------------------------------------------
// Create: Value
// -----------------------------------------------------------

const V = Value.Create(T)

console.log(V)

// -----------------------------------------------------------
// Compile: Type
// -----------------------------------------------------------

const C = TypeCompiler.Compile(T)

console.log(C.Code())

// -----------------------------------------------------------
// Check: Value
// -----------------------------------------------------------

console.log(C.Check(V))
17 changes: 16 additions & 1 deletion test/static/partial.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Expect } from './assert'
import { Type, Static } from '@sinclair/typebox'

import * as Types from '@sinclair/typebox'
{
const T = Type.Partial(
Type.Object({
Expand Down Expand Up @@ -70,3 +70,18 @@ import { Type, Static } from '@sinclair/typebox'
>()
}
}
{
// https://github.com/sinclairzx81/typebox/issues/655
const T = Type.Object({
a: Type.ReadonlyOptional(Type.Number()),
b: Type.Readonly(Type.Number()),
c: Type.Optional(Type.Number()),
d: Type.Number(),
})
const R: Types.TObject<{
a: Types.TReadonlyOptional<Types.TNumber>
b: Types.TReadonlyOptional<Types.TNumber>
c: Types.TOptional<Types.TNumber>
d: Types.TOptional<Types.TNumber>
}> = Type.Partial(T)
}
16 changes: 16 additions & 0 deletions test/static/required.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Expect } from './assert'
import { Type, Static } from '@sinclair/typebox'
import * as Types from '@sinclair/typebox'
{
const T = Type.Required(
Type.Object({
Expand Down Expand Up @@ -71,3 +72,18 @@ import { Type, Static } from '@sinclair/typebox'
>()
}
}
{
// https://github.com/sinclairzx81/typebox/issues/655
const T = Type.Object({
a: Type.ReadonlyOptional(Type.Number()),
b: Type.Readonly(Type.Number()),
c: Type.Optional(Type.Number()),
d: Type.Number(),
})
const R: Types.TObject<{
a: Types.TReadonly<Types.TNumber>
b: Types.TReadonly<Types.TNumber>
c: Types.TNumber
d: Types.TNumber
}> = Type.Required(T)
}

0 comments on commit a60d06e

Please sign in to comment.