0
# Advanced Types
1
2
TypeScript-inspired advanced types including utility types, conditional types, and template literals for complex schema composition. These types enable sophisticated type transformations and constraints.
3
4
## Capabilities
5
6
### Utility Types
7
8
#### Partial Type
9
10
Makes all properties of an object type optional.
11
12
```typescript { .api }
13
/**
14
* Makes all properties of an object type optional
15
* @param schema - Object schema to make partial
16
* @returns TPartial schema
17
*/
18
function Partial<T extends TSchema>(schema: T, options?: SchemaOptions): TPartial<T>;
19
```
20
21
**Usage Examples:**
22
23
```typescript
24
import { Type } from "@sinclair/typebox";
25
26
const User = Type.Object({
27
name: Type.String(),
28
age: Type.Number(),
29
email: Type.String()
30
});
31
32
// All properties become optional
33
const PartialUser = Type.Partial(User);
34
// Equivalent to: { name?: string, age?: number, email?: string }
35
```
36
37
#### Required Type
38
39
Makes all properties of an object type required.
40
41
```typescript { .api }
42
/**
43
* Makes all properties of an object type required
44
* @param schema - Object schema to make required
45
* @returns TRequired schema
46
*/
47
function Required<T extends TSchema>(schema: T, options?: SchemaOptions): TRequired<T>;
48
```
49
50
#### Pick Type
51
52
Picks specific properties from an object type.
53
54
```typescript { .api }
55
/**
56
* Picks specific properties from an object type
57
* @param schema - Object schema to pick from
58
* @param keys - Union of literal keys to pick
59
* @returns TPick schema
60
*/
61
function Pick<T extends TObject, K extends TUnion<TLiteral[]>>(schema: T, keys: K, options?: SchemaOptions): TPick<T, K>;
62
```
63
64
**Usage Examples:**
65
66
```typescript
67
const User = Type.Object({
68
id: Type.String(),
69
name: Type.String(),
70
email: Type.String(),
71
password: Type.String(),
72
createdAt: Type.Date()
73
});
74
75
// Pick only specific properties
76
const PublicUser = Type.Pick(User, Type.Union([
77
Type.Literal('id'),
78
Type.Literal('name'),
79
Type.Literal('email')
80
]));
81
// Result: { id: string, name: string, email: string }
82
```
83
84
#### Omit Type
85
86
Omits specific properties from an object type.
87
88
```typescript { .api }
89
/**
90
* Omits specific properties from an object type
91
* @param schema - Object schema to omit from
92
* @param keys - Union of literal keys to omit
93
* @returns TOmit schema
94
*/
95
function Omit<T extends TObject, K extends TUnion<TLiteral[]>>(schema: T, keys: K, options?: SchemaOptions): TOmit<T, K>;
96
```
97
98
**Usage Examples:**
99
100
```typescript
101
// Omit sensitive properties
102
const SafeUser = Type.Omit(User, Type.Union([
103
Type.Literal('password'),
104
Type.Literal('createdAt')
105
]);
106
// Result: { id: string, name: string, email: string }
107
```
108
109
#### Readonly Types
110
111
Makes properties readonly or both readonly and optional.
112
113
```typescript { .api }
114
/**
115
* Makes properties readonly
116
* @param schema - Schema to make readonly
117
* @returns TReadonly schema
118
*/
119
function Readonly<T extends TSchema>(schema: T): TReadonly<T>;
120
121
/**
122
* Makes properties both readonly and optional
123
* @param schema - Schema to make readonly and optional
124
* @returns TReadonlyOptional schema
125
*/
126
function ReadonlyOptional<T extends TSchema>(schema: T): TReadonlyOptional<T>;
127
```
128
129
### Record Type
130
131
Creates record types with key/value schemas.
132
133
```typescript { .api }
134
/**
135
* Creates record types with key/value schemas
136
* @param key - Schema for record keys
137
* @param value - Schema for record values
138
* @returns TRecord schema
139
*/
140
function Record<K extends TSchema, V extends TSchema>(key: K, value: V): TRecord<K, V>;
141
```
142
143
**Usage Examples:**
144
145
```typescript
146
// String keys, number values
147
const StringToNumber = Type.Record(Type.String(), Type.Number());
148
// Equivalent to: Record<string, number>
149
150
// Literal keys, object values
151
const UserRoles = Type.Record(
152
Type.Union([Type.Literal('admin'), Type.Literal('user'), Type.Literal('guest')]),
153
Type.Object({
154
permissions: Type.Array(Type.String()),
155
level: Type.Number()
156
})
157
);
158
```
159
160
### Intersect Type
161
162
Creates an intersection of multiple types (AND logic).
163
164
```typescript { .api }
165
/**
166
* Creates an intersection of multiple types (AND logic)
167
* @param schemas - Schemas to intersect
168
* @returns TIntersect schema
169
*/
170
function Intersect<T extends TSchema[]>(...schemas: [...T]): TIntersect<T>;
171
```
172
173
**Usage Examples:**
174
175
```typescript
176
const Person = Type.Object({
177
name: Type.String(),
178
age: Type.Number()
179
});
180
181
const Employee = Type.Object({
182
employeeId: Type.String(),
183
department: Type.String()
184
});
185
186
// Intersection combines both types
187
const PersonEmployee = Type.Intersect([Person, Employee]);
188
// Result: { name: string, age: number, employeeId: string, department: string }
189
```
190
191
### Conditional Types
192
193
#### Extends Type
194
195
Creates conditional types with ternary logic.
196
197
```typescript { .api }
198
/**
199
* Creates conditional types with ternary logic
200
* @param left - Type to check
201
* @param right - Type to check against
202
* @param trueType - Type when condition is true
203
* @param falseType - Type when condition is false
204
* @returns TExtends schema
205
*/
206
function Extends<L extends TSchema, R extends TSchema, T extends TSchema, F extends TSchema>(
207
left: L,
208
right: R,
209
trueType: T,
210
falseType: F
211
): TExtends<L, R, T, F>;
212
```
213
214
#### Exclude and Extract Types
215
216
Utility types for union manipulation.
217
218
```typescript { .api }
219
/**
220
* Excludes types from union
221
* @param from - Union to exclude from
222
* @param exclude - Types to exclude
223
* @returns TExclude schema
224
*/
225
function Exclude<T extends TSchema, U extends TSchema>(from: T, exclude: U): TExclude<T, U>;
226
227
/**
228
* Extracts matching types from union
229
* @param from - Union to extract from
230
* @param extract - Types to extract
231
* @returns TExtract schema
232
*/
233
function Extract<T extends TSchema, U extends TSchema>(from: T, extract: U): TExtract<T, U>;
234
```
235
236
### KeyOf Type
237
238
Creates union of object property keys.
239
240
```typescript { .api }
241
/**
242
* Creates union of object property keys
243
* @param schema - Object schema to extract keys from
244
* @returns TKeyOf schema
245
*/
246
function KeyOf<T extends TObject>(schema: T): TKeyOf<T>;
247
```
248
249
**Usage Examples:**
250
251
```typescript
252
const User = Type.Object({
253
id: Type.String(),
254
name: Type.String(),
255
email: Type.String()
256
});
257
258
const UserKeys = Type.KeyOf(User);
259
// Result: "id" | "name" | "email"
260
```
261
262
### Template Literal Types
263
264
Creates template literal string types for pattern matching.
265
266
```typescript { .api }
267
/**
268
* Creates template literal string types
269
* @param kinds - Array of string literals and type patterns
270
* @returns TTemplateLiteral schema
271
*/
272
function TemplateLiteral<T extends TTemplateLiteralKind[]>(kinds: [...T]): TTemplateLiteral<T>;
273
274
type TTemplateLiteralKind = TLiteral<string> | TString | TNumber | TBoolean;
275
```
276
277
**Usage Examples:**
278
279
```typescript
280
// Simple template literal
281
const Greeting = Type.TemplateLiteral([
282
Type.Literal('Hello '),
283
Type.String()
284
]);
285
// Matches: "Hello world", "Hello Alice", etc.
286
287
// Complex template literal
288
const EventName = Type.TemplateLiteral([
289
Type.Literal('on'),
290
Type.Union([Type.Literal('Click'), Type.Literal('Hover'), Type.Literal('Focus')]),
291
Type.String()
292
]);
293
// Matches: "onClickButton", "onHoverMenu", "onFocusInput", etc.
294
```
295
296
### Mapped Types
297
298
Creates mapped object types with transformations.
299
300
```typescript { .api }
301
/**
302
* Creates mapped object types with transformations
303
* @param key - Union of keys to map over
304
* @param schema - Schema to apply to each key
305
* @returns TMapped schema
306
*/
307
function Mapped<K extends TUnion<TLiteral[]>, T extends TSchema>(key: K, schema: T): TMapped<K, T>;
308
```
309
310
### Index Types
311
312
Creates indexed access types.
313
314
```typescript { .api }
315
/**
316
* Creates indexed access types
317
* @param schema - Object schema to index into
318
* @param key - Key to access
319
* @returns TIndex schema
320
*/
321
function Index<T extends TObject, K extends TSchema>(schema: T, key: K): TIndex<T, K>;
322
```
323
324
### Intrinsic String Functions
325
326
String transformation utilities for literal types.
327
328
```typescript { .api }
329
/**
330
* Capitalizes string literal types
331
* @param schema - String literal schema
332
* @returns TCapitalize schema
333
*/
334
function Capitalize<T extends TString>(schema: T): TCapitalize<T>;
335
336
/**
337
* Lowercases string literal types
338
* @param schema - String literal schema
339
* @returns TLowercase schema
340
*/
341
function Lowercase<T extends TString>(schema: T): TLowercase<T>;
342
343
/**
344
* Uppercases string literal types
345
* @param schema - String literal schema
346
* @returns TUppercase schema
347
*/
348
function Uppercase<T extends TString>(schema: T): TUppercase<T>;
349
350
/**
351
* Uncapitalizes string literal types
352
* @param schema - String literal schema
353
* @returns TUncapitalize schema
354
*/
355
function Uncapitalize<T extends TString>(schema: T): TUncapitalize<T>;
356
```
357
358
### Tuple Utilities
359
360
Utilities for working with tuple types.
361
362
```typescript { .api }
363
/**
364
* Extracts rest elements from Tuple/Intersect/Union
365
* @param schema - Schema to extract from
366
* @returns TRest schema
367
*/
368
function Rest<T extends TSchema>(schema: T): TRest<T>;
369
```
370
371
### Not Type
372
373
Creates negation types (NOT logic).
374
375
```typescript { .api }
376
/**
377
* Creates negation types (NOT logic)
378
* @param schema - Schema to negate
379
* @returns TNot schema
380
*/
381
function Not<T extends TSchema>(schema: T): TNot<T>;
382
```
383
384
**Usage Examples:**
385
386
```typescript
387
// Not a string (anything except string)
388
const NotString = Type.Not(Type.String());
389
390
// Not a specific value
391
const NotZero = Type.Not(Type.Literal(0));
392
```
393
394
## Type Interfaces
395
396
```typescript { .api }
397
interface TPartial<T extends TSchema> extends TSchema {
398
[Kind]: 'Partial';
399
type: 'object';
400
properties: { [K in keyof T['properties']]: TOptional<T['properties'][K]> };
401
}
402
403
interface TRequired<T extends TSchema> extends TSchema {
404
[Kind]: 'Required';
405
type: 'object';
406
properties: T['properties'];
407
required: (keyof T['properties'])[];
408
}
409
410
interface TPick<T extends TObject, K extends TUnion<TLiteral[]>> extends TSchema {
411
[Kind]: 'Pick';
412
type: 'object';
413
properties: Pick<T['properties'], K>;
414
}
415
416
interface TOmit<T extends TObject, K extends TUnion<TLiteral[]>> extends TSchema {
417
[Kind]: 'Omit';
418
type: 'object';
419
properties: Omit<T['properties'], K>;
420
}
421
422
interface TIntersect<T extends TSchema[]> extends TSchema {
423
allOf: T;
424
}
425
426
interface TRecord<K extends TSchema, V extends TSchema> extends TSchema {
427
type: 'object';
428
patternProperties: {
429
[pattern: string]: V;
430
};
431
}
432
433
interface TKeyOf<T extends TObject> extends TSchema {
434
anyOf: TLiteral<keyof T['properties']>[];
435
}
436
437
interface TTemplateLiteral<T extends TTemplateLiteralKind[]> extends TSchema {
438
type: 'string';
439
pattern: string;
440
}
441
442
interface TNot<T extends TSchema> extends TSchema {
443
not: T;
444
}
445
```