0
# Any Module
1
2
Generic type utilities for fundamental type operations including casting, equality checking, property access, and type computation control.
3
4
## Capabilities
5
6
### Type Casting
7
8
Cast types with fallback behavior when casting fails.
9
10
```typescript { .api }
11
/**
12
* Cast type A1 to A2, falling back to A2 if cast fails
13
* @param A1 - Type to cast from
14
* @param A2 - Type to cast to
15
* @returns A1 if it extends A2, otherwise A2
16
*/
17
type Cast<A1, A2> = A1 extends A2 ? A1 : A2;
18
```
19
20
**Usage Examples:**
21
22
```typescript
23
import { A } from "ts-toolbelt";
24
25
type Result1 = A.Cast<"hello", string>; // "hello"
26
type Result2 = A.Cast<42, string>; // string
27
type Result3 = A.Cast<{ a: 1 }, { a: number }>; // { a: 1 }
28
```
29
30
### Type Equality and Relationships
31
32
Check strict type equality and extends relationships between types.
33
34
```typescript { .api }
35
/**
36
* Check if two types are strictly equal
37
* @param A1 - First type to compare
38
* @param A2 - Second type to compare
39
* @returns 1 if types are equal, 0 otherwise
40
*/
41
type Equals<A1, A2> = (<A>() => A extends A1 ? 1 : 0) extends (<A>() => A extends A2 ? 1 : 0) ? 1 : 0;
42
43
/**
44
* Check if type A extends type B
45
* @param A - Type to check
46
* @param B - Type to check against
47
* @returns 1 if A extends B, 0 otherwise
48
*/
49
type Extends<A, B> = A extends B ? 1 : 0;
50
51
/**
52
* Check if type A contains type B
53
* @param A - Container type
54
* @param B - Type to look for
55
* @returns 1 if A contains B, 0 otherwise
56
*/
57
type Contains<A, B> = B extends A ? 1 : 0;
58
```
59
60
**Usage Examples:**
61
62
```typescript
63
import { A } from "ts-toolbelt";
64
65
type Equal1 = A.Equals<string, string>; // 1
66
type Equal2 = A.Equals<string, number>; // 0
67
type Equal3 = A.Equals<"hello", string>; // 0
68
69
type Extends1 = A.Extends<"hello", string>; // 1
70
type Extends2 = A.Extends<string, "hello">; // 0
71
type Extends3 = A.Extends<never, any>; // 1
72
73
type Contains1 = A.Contains<string | number, string>; // 1
74
type Contains2 = A.Contains<string, number>; // 0
75
```
76
77
### Property Access
78
79
Access properties and extract keys from types.
80
81
```typescript { .api }
82
/**
83
* Describes index keys for any type
84
* @returns Union of string, number, and symbol
85
*/
86
type Key = string | number | symbol;
87
88
/**
89
* Access property at key K in type O
90
* @param O - Object type to access
91
* @param K - Key to access
92
* @returns Property type at key K
93
*/
94
type At<O, K extends keyof O> = O[K];
95
96
/**
97
* Extract all keys from type O
98
* @param O - Type to extract keys from
99
* @returns Union of all keys
100
*/
101
type Keys<O> = keyof O;
102
103
/**
104
* Extract known keys (non-index signature) from type O
105
* @param O - Type to extract known keys from
106
* @returns Union of known keys
107
*/
108
type KnownKeys<O> = keyof O;
109
```
110
111
**Usage Examples:**
112
113
```typescript
114
import { A } from "ts-toolbelt";
115
116
type User = { name: string; age: number; email: string };
117
118
// Key type for valid property keys
119
type ValidKey = A.Key; // string | number | symbol
120
type StringKey: A.Key = "name"; // Valid
121
type NumberKey: A.Key = 0; // Valid
122
type SymbolKey: A.Key = Symbol("key"); // Valid
123
124
type Name = A.At<User, "name">; // string
125
type UserKeys = A.Keys<User>; // "name" | "age" | "email"
126
type KnownUserKeys = A.KnownKeys<User>; // "name" | "age" | "email"
127
128
// With index signatures
129
type IndexedType = { [key: string]: any; knownProp: number };
130
type IndexedKeys = A.Keys<IndexedType>; // string | "knownProp"
131
type KnownKeys = A.KnownKeys<IndexedType>; // "knownProp"
132
```
133
134
### Type Computation
135
136
Control TypeScript's type computation and evaluation.
137
138
```typescript { .api }
139
/**
140
* Force TypeScript to evaluate intersections and compute final type
141
* @param O - Object type to compute
142
* @param depth - Computation depth: 'flat' or 'deep'
143
* @returns Computed type with forced evaluation
144
*/
145
type Compute<O extends Record<string, any>, depth extends 'flat' | 'deep' = 'flat'> =
146
depth extends 'flat' ? { [K in keyof O]: O[K] } & {} : ComputeDeep<O>;
147
```
148
149
**Usage Examples:**
150
151
```typescript
152
import { A } from "ts-toolbelt";
153
154
type Base = { a: number };
155
type Extended = Base & { b: string };
156
157
type Computed = A.Compute<Extended>; // { a: number; b: string }
158
159
// Deep computation for nested intersections
160
type NestedIntersection = {
161
user: { name: string } & { age: number };
162
data: { id: string } & { value: number };
163
};
164
165
type DeepComputed = A.Compute<NestedIntersection, 'deep'>;
166
// {
167
// user: { name: string; age: number };
168
// data: { id: string; value: number };
169
// }
170
```
171
172
### Promise Operations
173
174
Handle promise types and async operations.
175
176
```typescript { .api }
177
/**
178
* Await/unwrap promise types
179
* @param P - Promise type to await
180
* @returns Resolved value type of promise
181
*/
182
type Await<P> = P extends Promise<infer T> ? T : P;
183
184
/**
185
* Promise-related type utilities
186
*/
187
type Promise<T> = globalThis.Promise<T>;
188
```
189
190
**Usage Examples:**
191
192
```typescript
193
import { A } from "ts-toolbelt";
194
195
type AsyncString = Promise<string>;
196
type SyncString = A.Await<AsyncString>; // string
197
198
type AsyncUser = Promise<{ name: string; age: number }>;
199
type User = A.Await<AsyncUser>; // { name: string; age: number }
200
201
// Works with non-promises too
202
type AlreadySync = A.Await<number>; // number
203
```
204
205
### Type Utilities
206
207
Additional type checking and manipulation utilities.
208
209
```typescript { .api }
210
/**
211
* Generic type checking utility
212
* @param A - Type to check
213
* @param C - Condition/constraint
214
* @returns Type checking result
215
*/
216
type Is<A, C> = A extends C ? 1 : 0;
217
218
/**
219
* Error handling for types - try type operations safely
220
* @param A - Type to try operation on
221
* @param Catch - Fallback type if operation fails
222
* @returns Result of operation or fallback
223
*/
224
type Try<A, Catch = never> = A extends never ? Catch : A;
225
226
/**
227
* Placeholder type for currying operations
228
*/
229
type x = any;
230
231
/**
232
* Basic type operations
233
*/
234
type Type = any;
235
```
236
237
**Usage Examples:**
238
239
```typescript
240
import { A } from "ts-toolbelt";
241
242
// Type checking
243
type IsString = A.Is<"hello", string>; // 1
244
type IsNumber = A.Is<"hello", number>; // 0
245
246
// Safe type operations
247
type SafeOperation = A.Try<string, "fallback">; // string
248
type FailedOperation = A.Try<never, "fallback">; // "fallback"
249
250
// Currying placeholder
251
type CurriedFn<T> = T extends A.x ? number : T;
252
type Result1 = CurriedFn<A.x>; // number
253
type Result2 = CurriedFn<string>; // string
254
```
255
256
## Types
257
258
```typescript { .api }
259
// Core types used by Any module
260
type Key = string | number | symbol;
261
```