0
# Misc Module
1
2
Miscellaneous utilities including built-in type definitions, primitive types, and JSON handling capabilities.
3
4
## Capabilities
5
6
### Built-in Types
7
8
Definitions for TypeScript's built-in object types.
9
10
```typescript { .api }
11
/**
12
* Union of TypeScript's built-in object types
13
*/
14
type BuiltIn = Function | Error | Date | { readonly [Symbol.toStringTag]: string } | RegExp | Generator;
15
```
16
17
**Usage Examples:**
18
19
```typescript
20
import { Misc } from "ts-toolbelt";
21
22
// Check if type extends built-in
23
type IsBuiltIn<T> = T extends Misc.BuiltIn ? true : false;
24
25
type DateCheck = IsBuiltIn<Date>; // true
26
type ErrorCheck = IsBuiltIn<Error>; // true
27
type RegExpCheck = IsBuiltIn<RegExp>; // true
28
type FunctionCheck = IsBuiltIn<Function>; // true
29
type CustomCheck = IsBuiltIn<{ name: string }>; // false
30
31
// Filter built-ins from union
32
type Mixed = Date | string | RegExp | number | Error;
33
type OnlyBuiltIns = Extract<Mixed, Misc.BuiltIn>; // Date | RegExp | Error
34
type NoBuiltIns = Exclude<Mixed, Misc.BuiltIn>; // string | number
35
```
36
37
### Primitive Types
38
39
Definition of JavaScript primitive types.
40
41
```typescript { .api }
42
/**
43
* Union of JavaScript primitive types
44
*/
45
type Primitive = string | number | boolean | bigint | symbol | undefined | null;
46
```
47
48
**Usage Examples:**
49
50
```typescript
51
import { Misc } from "ts-toolbelt";
52
53
// Check if type is primitive
54
type IsPrimitive<T> = T extends Misc.Primitive ? true : false;
55
56
type StringCheck = IsPrimitive<string>; // true
57
type NumberCheck = IsPrimitive<number>; // true
58
type BooleanCheck = IsPrimitive<boolean>; // true
59
type BigIntCheck = IsPrimitive<bigint>; // true
60
type SymbolCheck = IsPrimitive<symbol>; // true
61
type NullCheck = IsPrimitive<null>; // true
62
type UndefinedCheck = IsPrimitive<undefined>; // true
63
64
type ObjectCheck = IsPrimitive<{}>; // false
65
type ArrayCheck = IsPrimitive<[]>; // false
66
type FunctionCheck = IsPrimitive<() => void>; // false
67
68
// Filter primitives from complex types
69
type ComplexUnion = string | { name: string } | number | Date | boolean;
70
type OnlyPrimitives = Extract<ComplexUnion, Misc.Primitive>; // string | number | boolean
71
type OnlyObjects = Exclude<ComplexUnion, Misc.Primitive>; // { name: string } | Date
72
```
73
74
### JSON Utilities
75
76
Specialized types for working with JSON data structures.
77
78
```typescript { .api }
79
/**
80
* JSON namespace containing JSON-specific type utilities
81
*/
82
namespace JSON {
83
/**
84
* Valid JSON primitive values
85
*/
86
type Primitive = string | number | boolean | null;
87
88
/**
89
* JSON array type (recursive)
90
*/
91
type Array = Value[];
92
93
/**
94
* JSON object type (recursive)
95
*/
96
type Object = { [key: string]: Value };
97
98
/**
99
* Any valid JSON value
100
*/
101
type Value = Primitive | Object | Array;
102
}
103
```
104
105
**Usage Examples:**
106
107
```typescript
108
import { Misc } from "ts-toolbelt";
109
110
// JSON type validation
111
type ValidJsonValue<T> = T extends Misc.JSON.Value ? T : never;
112
113
type ValidString = ValidJsonValue<string>; // string
114
type ValidNumber = ValidJsonValue<number>; // number
115
type ValidBoolean = ValidJsonValue<boolean>; // boolean
116
type ValidNull = ValidJsonValue<null>; // null
117
type ValidObject = ValidJsonValue<{ name: string; age: number }>; // { name: string; age: number }
118
type ValidArray = ValidJsonValue<string[]>; // string[]
119
120
// Invalid JSON types
121
type InvalidUndefined = ValidJsonValue<undefined>; // never
122
type InvalidFunction = ValidJsonValue<() => void>; // never
123
type InvalidSymbol = ValidJsonValue<symbol>; // never
124
type InvalidBigInt = ValidJsonValue<bigint>; // never
125
126
// JSON-safe object creation
127
type JsonSafeObject<T> = {
128
[K in keyof T]: T[K] extends Misc.JSON.Value ? T[K] : never;
129
};
130
131
type User = {
132
name: string;
133
age: number;
134
isActive: boolean;
135
callback: () => void; // Invalid in JSON
136
metadata: { created: Date }; // Date invalid in JSON
137
};
138
139
type SafeUser = JsonSafeObject<User>;
140
// {
141
// name: string;
142
// age: number;
143
// isActive: boolean;
144
// callback: never;
145
// metadata: never;
146
// }
147
148
// API response typing
149
type ApiResponse<T extends Misc.JSON.Value> = {
150
success: boolean;
151
data: T;
152
timestamp: number;
153
meta: Misc.JSON.Object;
154
};
155
156
type UserResponse = ApiResponse<{
157
id: string;
158
name: string;
159
preferences: {
160
theme: "light" | "dark";
161
notifications: boolean;
162
};
163
}>;
164
165
// JSON parsing type safety
166
type ParseResult<T extends string> = T extends `${infer _}`
167
? Misc.JSON.Value
168
: never;
169
170
// Configuration management
171
type Config = {
172
database: {
173
host: string;
174
port: number;
175
ssl: boolean;
176
};
177
features: {
178
auth: boolean;
179
logging: boolean;
180
};
181
metadata: Misc.JSON.Object;
182
};
183
184
type IsConfigJsonSafe = Config extends { [K in keyof Config]: Misc.JSON.Value } ? true : false; // true
185
186
// Deep JSON validation
187
type DeepJsonSafe<T> = T extends Misc.JSON.Primitive
188
? T
189
: T extends (infer U)[]
190
? DeepJsonSafe<U>[]
191
: T extends Record<string, any>
192
? { [K in keyof T]: DeepJsonSafe<T[K]> }
193
: never;
194
195
type NestedConfig = {
196
app: {
197
name: string;
198
version: string;
199
settings: {
200
debug: boolean;
201
timeout: number;
202
};
203
};
204
};
205
206
type SafeNestedConfig = DeepJsonSafe<NestedConfig>; // Same as NestedConfig (all JSON-safe)
207
```
208
209
### Practical Applications
210
211
Real-world usage patterns for miscellaneous utilities.
212
213
**Usage Examples:**
214
215
```typescript
216
import { Misc, A } from "ts-toolbelt";
217
218
// Serialization type checking
219
type Serializable<T> = T extends Misc.Primitive
220
? T
221
: T extends Misc.BuiltIn
222
? never
223
: T extends (infer U)[]
224
? Serializable<U>[]
225
: T extends Record<string, any>
226
? { [K in keyof T]: Serializable<T[K]> }
227
: never;
228
229
type Data = {
230
id: string;
231
createdAt: Date; // Not serializable
232
tags: string[];
233
metadata: { version: number };
234
};
235
236
type SerializableData = Serializable<Data>;
237
// {
238
// id: string;
239
// createdAt: never;
240
// tags: string[];
241
// metadata: { version: number };
242
// }
243
244
// Environment configuration validation
245
type EnvValue = string | number | boolean;
246
type EnvConfig = Record<string, EnvValue>;
247
248
type IsEnvSafe<T> = T extends EnvValue ? true : false;
249
250
type DatabaseConfig = {
251
DB_HOST: string;
252
DB_PORT: number;
253
DB_SSL: boolean;
254
DB_POOL: { min: number; max: number }; // Not env-safe (object)
255
};
256
257
type EnvSafeCheck = {
258
[K in keyof DatabaseConfig]: IsEnvSafe<DatabaseConfig[K]>;
259
};
260
// {
261
// DB_HOST: true;
262
// DB_PORT: true;
263
// DB_SSL: true;
264
// DB_POOL: false;
265
// }
266
267
// Type filtering utilities
268
type FilterPrimitives<T> = T extends Misc.Primitive ? T : never;
269
type FilterBuiltIns<T> = T extends Misc.BuiltIn ? T : never;
270
type FilterJsonValues<T> = T extends Misc.JSON.Value ? T : never;
271
272
type MixedTypes = string | Date | number | RegExp | boolean | { id: string } | null;
273
274
type OnlyPrimitives = FilterPrimitives<MixedTypes>; // string | number | boolean | null
275
type OnlyBuiltIns = FilterBuiltIns<MixedTypes>; // Date | RegExp
276
type OnlyJsonValues = FilterJsonValues<MixedTypes>; // string | number | boolean | { id: string } | null
277
```
278
279
## Types
280
281
```typescript { .api }
282
// Core miscellaneous types
283
type BuiltIn = Function | Error | Date | { readonly [Symbol.toStringTag]: string } | RegExp | Generator;
284
type Primitive = string | number | boolean | bigint | symbol | undefined | null;
285
286
// JSON namespace types
287
namespace JSON {
288
type Primitive = string | number | boolean | null;
289
type Array = Value[];
290
type Object = { [key: string]: Value };
291
type Value = Primitive | Object | Array;
292
}
293
```