|
1 | 1 | <h1>Script</h1> |
2 | | -<p>Parse TypeScript into Json Schema</p> |
| 2 | +<p>TypeScript Scripting Engine</p> |
3 | 3 | <h2>Overview</h2> |
4 | | -<p>TypeBox can parse TypeScript syntax into Json Schema. The Script function is designed to be syntactic frontend to the TypeBox type builder API, enabling Json Schema to be both constructed and mapped using TypeScript type expressions encoded in template literal strings.</p> |
5 | | -<h3>Example</h3> |
6 | | -<p>The following uses Script to construct and map Json Schema.</p> |
7 | | -<pre><code class="language-typescript">import Type from 'typebox' |
| 4 | +<p>TypeBox includes a TypeScript scripting engine able to parse TypeScript types directly into Json Schema. The engine uses symmetric runtime and type-level parsing and returns typed safe schematics from TypeScript types (including computed types). The engine is designed for TypeScript 7 native compiler but is supported in TypeScript 5 and above.</p> |
| 5 | +<pre><code class="language-typescript">// Scripted Type |
8 | 6 |
|
9 | 7 | const T = Type.Script(`{ |
10 | 8 | x: number, |
11 | | - y: number, |
12 | | - z: number |
13 | | -}`) // const T = { |
14 | | - // type: 'object', |
15 | | - // required: ['x', 'y', 'z'], |
16 | | - // properties: { |
17 | | - // x: { type: 'number' }, |
18 | | - // y: { type: 'number' }, |
19 | | - // z: { type: 'number' } |
20 | | - // } |
21 | | - // } |
| 9 | + y: string, |
| 10 | + z: boolean |
| 11 | +}`) // const T: TObject<{ |
| 12 | + // x: TNumber, |
| 13 | + // y: TString, |
| 14 | + // z: TBoolean |
| 15 | + // }> |
| 16 | + |
| 17 | +// Json Schema Introspection |
| 18 | + |
| 19 | +T.type // 'object' |
| 20 | +T.required // ['x', 'y', 'z'] |
| 21 | +T.properties // { x: ..., y: ..., z: ... } |
| 22 | + |
| 23 | +// Scripted Type (Computed) |
22 | 24 |
|
23 | 25 | const S = Type.Script({ T }, `{ |
24 | 26 | [K in keyof T]: T[K] | null |
25 | | -}`) // const S = { |
26 | | - // type: 'object', |
27 | | - // required: ['x', 'y', 'z'], |
28 | | - // properties: { |
29 | | - // x: { |
30 | | - // anyOf: [ |
31 | | - // { type: 'number' }, |
32 | | - // { type: 'null' } |
33 | | - // ] |
34 | | - // }, |
35 | | - // y: { |
36 | | - // anyOf: [ |
37 | | - // { type: 'number' }, |
38 | | - // { type: 'null' } |
39 | | - // ] |
40 | | - // }, |
41 | | - // z: { |
42 | | - // anyOf: [ |
43 | | - // { type: 'number' }, |
44 | | - // { type: 'null' } |
45 | | - // ] |
46 | | - // }, |
47 | | - // } |
48 | | - // } |
| 27 | +}`) // const S: TObject<{ |
| 28 | + // x: TUnion<[TNumber, TNull]>, |
| 29 | + // y: TUnion<[TString, TNull]>, |
| 30 | + // z: TUnion<[TBoolean, TNull]> |
| 31 | + // }> |
| 32 | + |
| 33 | +// Standard Inference |
49 | 34 |
|
50 | 35 | type S = Type.Static<typeof S> // type S = { |
51 | 36 | // x: number | null, |
52 | | - // y: number | null, |
53 | | - // z: number | null |
| 37 | + // y: string | null, |
| 38 | + // z: boolean | null |
54 | 39 | // } |
55 | 40 | </code></pre> |
0 commit comments