0
# Additional Namespaces
1
2
Additional TypeBox namespaces that provide specialized functionality for parsing, system configuration, and module management.
3
4
## Capabilities
5
6
### Parser Namespace
7
8
Schema parsing and validation utilities for working with external schema definitions.
9
10
```typescript { .api }
11
/**
12
* Runtime schema parsing utilities
13
*/
14
namespace Runtime {
15
function Parse<T extends TSchema>(schema: T, value: unknown): Static<T>;
16
function Check<T extends TSchema>(schema: T, value: unknown): value is Static<T>;
17
}
18
19
/**
20
* Static schema parsing utilities
21
*/
22
namespace Static {
23
function Parse<T extends TSchema>(schema: T, value: unknown): Static<T>;
24
function Check<T extends TSchema>(schema: T, value: unknown): value is Static<T>;
25
}
26
```
27
28
**Usage Examples:**
29
30
```typescript
31
import { Runtime, Static } from "@sinclair/typebox/parser";
32
33
const UserSchema = Type.Object({
34
name: Type.String(),
35
age: Type.Number()
36
});
37
38
// Runtime parsing
39
const runtimeUser = Runtime.Parse(UserSchema, userData);
40
41
// Static parsing
42
const staticUser = Static.Parse(UserSchema, userData);
43
```
44
45
### System Namespace
46
47
System-level configuration and policy management for TypeBox behavior.
48
49
```typescript { .api }
50
/**
51
* TypeSystem policy configuration
52
*/
53
interface TypeSystemPolicy {
54
/** Allow numeric strings in number validation */
55
allowNumericString: boolean;
56
/** Allow array-like objects in array validation */
57
allowArrayLike: boolean;
58
/** Allow void undefined policy */
59
allowVoidUndefined: boolean;
60
}
61
62
/**
63
* Configure TypeSystem policies
64
* @param policy - Policy configuration object
65
*/
66
function Policy(policy: Partial<TypeSystemPolicy>): void;
67
68
/**
69
* Get current TypeSystem policy
70
* @returns Current policy configuration
71
*/
72
function GetPolicy(): TypeSystemPolicy;
73
```
74
75
**Usage Examples:**
76
77
```typescript
78
import { TypeSystem } from "@sinclair/typebox/system";
79
80
// Configure system behavior
81
TypeSystem.Policy({
82
allowNumericString: true, // "123" validates as number
83
allowArrayLike: false, // Only true arrays allowed
84
allowVoidUndefined: true // void can be undefined
85
});
86
87
// Check current policy
88
const currentPolicy = TypeSystem.GetPolicy();
89
console.log(currentPolicy.allowNumericString); // true
90
```
91
92
### Module Type
93
94
Creates module types for complex schema organization and namespace management.
95
96
```typescript { .api }
97
/**
98
* Creates a module type for schema organization
99
* @param properties - Module property schemas
100
* @param options - Schema options
101
* @returns TModule schema
102
*/
103
function Module<T extends TProperties>(properties: T, options?: SchemaOptions): TModule<T>;
104
```
105
106
**Usage Examples:**
107
108
```typescript
109
// Define a module with related schemas
110
const UserModule = Type.Module({
111
User: Type.Object({
112
id: Type.String(),
113
name: Type.String(),
114
email: Type.String({ format: 'email' })
115
}),
116
CreateUserRequest: Type.Object({
117
name: Type.String(),
118
email: Type.String({ format: 'email' })
119
}),
120
UpdateUserRequest: Type.Partial(Type.Object({
121
name: Type.String(),
122
email: Type.String({ format: 'email' })
123
}))
124
});
125
126
// Use module schemas
127
type User = Static<typeof UserModule.properties.User>;
128
type CreateUserRequest = Static<typeof UserModule.properties.CreateUserRequest>;
129
```
130
131
### Recursive Types
132
133
Advanced recursive type definitions for self-referencing schemas.
134
135
```typescript { .api }
136
/**
137
* Creates recursive schema types
138
* @param callback - Function defining recursive structure
139
* @param options - Schema options
140
* @returns TRecursive schema
141
*/
142
function Recursive<T extends TSchema>(
143
callback: (thisType: TThis) => T,
144
options?: SchemaOptions
145
): TRecursive<T>;
146
147
/**
148
* Reference to self in recursive definitions
149
*/
150
interface TThis extends TSchema {
151
[Kind]: 'This';
152
}
153
```
154
155
**Usage Examples:**
156
157
```typescript
158
// Self-referencing tree structure
159
const TreeNode = Type.Recursive(This => Type.Object({
160
value: Type.String(),
161
children: Type.Array(This)
162
}));
163
164
// Linked list structure
165
const LinkedList = Type.Recursive(This => Type.Object({
166
data: Type.Any(),
167
next: Type.Union([This, Type.Null()])
168
}));
169
170
// JSON-like recursive structure
171
const JsonValue = Type.Recursive(This => Type.Union([
172
Type.String(),
173
Type.Number(),
174
Type.Boolean(),
175
Type.Null(),
176
Type.Array(This),
177
Type.Object({}, { additionalProperties: This })
178
]));
179
```
180
181
### Reference Types
182
183
Schema references for modular schema composition and reuse.
184
185
```typescript { .api }
186
/**
187
* Creates a reference to another schema by ID
188
* @param ref - Schema reference ID
189
* @param options - Schema options
190
* @returns TRef schema
191
*/
192
function Ref<T extends TSchema = TSchema>(ref: string, options?: SchemaOptions): TRef<T>;
193
194
/**
195
* Creates a schema with an ID for referencing
196
* @param id - Schema identifier
197
* @param schema - Schema to identify
198
* @returns Schema with $id property
199
*/
200
function Id<T extends TSchema>(id: string, schema: T): T & { $id: string };
201
```
202
203
**Usage Examples:**
204
205
```typescript
206
// Define reusable schemas with IDs
207
const PersonSchema = Type.Id('Person', Type.Object({
208
name: Type.String(),
209
age: Type.Number()
210
}));
211
212
const AddressSchema = Type.Id('Address', Type.Object({
213
street: Type.String(),
214
city: Type.String(),
215
country: Type.String()
216
}));
217
218
// Reference schemas by ID
219
const CustomerSchema = Type.Object({
220
person: Type.Ref<typeof PersonSchema>('Person'),
221
address: Type.Ref<typeof AddressSchema>('Address'),
222
customerSince: Type.Date()
223
});
224
225
// Use with references array in validation
226
const isValid = Value.Check(CustomerSchema, customerData, [
227
PersonSchema,
228
AddressSchema
229
]);
230
```
231
232
## Type Interfaces
233
234
```typescript { .api }
235
interface TModule<T extends TProperties> extends TSchema {
236
type: 'object';
237
properties: T;
238
[Kind]: 'Module';
239
}
240
241
interface TRecursive<T extends TSchema> extends TSchema {
242
[Kind]: 'Recursive';
243
}
244
245
interface TThis extends TSchema {
246
[Kind]: 'This';
247
}
248
249
interface TRef<T extends TSchema = TSchema> extends TSchema {
250
$ref: string;
251
[Kind]: 'Ref';
252
}
253
254
interface TypeSystemPolicy {
255
allowNumericString: boolean;
256
allowArrayLike: boolean;
257
allowVoidUndefined: boolean;
258
}
259
```