Skip to content

Commit 38934cb

Browse files
committed
Dependent If Type
1 parent 2690916 commit 38934cb

1 file changed

Lines changed: 58 additions & 15 deletions

File tree

example/index.ts

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,70 @@
1+
import Compile from 'typebox/compile'
2+
import System from 'typebox/system'
3+
import Guard from 'typebox/guard'
4+
import Format from 'typebox/format'
5+
import Schema from 'typebox/schema'
6+
import Value from 'typebox/value'
17
import Type from 'typebox'
28

3-
const { A } = Type.Script(`
4-
type A = {
5-
x: number
6-
y: number
7-
} & if { x: 1 } then { y: 2 } else
8-
if { x: 2 } then { y: 3 } else
9-
never
10-
`)
9+
// ------------------------------------------------------------------
10+
// Settings
11+
// ------------------------------------------------------------------
1112

13+
System.Settings.Set({ enumerableKind: false })
1214

13-
type A = Type.Static<typeof A>
15+
// ------------------------------------------------------------------
16+
// Guard
17+
// ------------------------------------------------------------------
1418

15-
function test(value: Type.Static<typeof A>) {}
19+
const A = Guard.GraphemeCount('type-📦') // 6
20+
const B = Guard.HasPropertyKey({ x: 1 }, 'x') // true
1621

17-
test({ x: 1, y: 2 }) // ok
18-
test({ x: 2, y: 3 }) // ok
22+
// ------------------------------------------------------------------
23+
// Type
24+
// ------------------------------------------------------------------
1925

20-
test({ x: 1, y: -1 }) // error: Type '-1' is not assignable to type '2 | 3'
21-
test({ x: 2, y: -1 }) // error: Type '-1' is not assignable to type '2 | 3'
26+
const T = Type.Object({
27+
x: Type.Number(),
28+
y: Type.Number(),
29+
z: Type.Number()
30+
})
2231

32+
// ------------------------------------------------------------------
33+
// Script
34+
// ------------------------------------------------------------------
2335

36+
const S = Type.Script({ T }, `{
37+
[K in keyof T]: T[K] | null
38+
}`)
2439

25-
console.dir(A)
40+
// ------------------------------------------------------------------
41+
// Infer
42+
// ------------------------------------------------------------------
2643

44+
type T = Type.Static<typeof T>
45+
type S = Type.Static<typeof S>
2746

47+
// ------------------------------------------------------------------
48+
// Parse
49+
// ------------------------------------------------------------------
50+
51+
const R = Value.Parse(T, { x: 1, y: 2, z: 3 })
52+
53+
// ------------------------------------------------------------------
54+
// Compile
55+
// ------------------------------------------------------------------
56+
const C = Compile(S)
57+
58+
const X = C.Parse({ x: 1, y: 2, z: 3 })
59+
60+
// ------------------------------------------------------------------
61+
// Format
62+
// ------------------------------------------------------------------
63+
64+
const E = Format.IsEmail('user@domain.com')
65+
66+
// ------------------------------------------------------------------
67+
// Schema
68+
// ------------------------------------------------------------------
69+
70+
const D = Schema.Parse({ const: 'hello' }, 'hello')

0 commit comments

Comments
 (0)