0
# Class Module
1
2
Class-related utilities for extracting instance types, constructor parameters, and working with class constructors in TypeScript's type system.
3
4
## Capabilities
5
6
### Class Type Definition
7
8
Base class type representing constructor functions.
9
10
```typescript { .api }
11
/**
12
* Generic class constructor type
13
* @param A - Instance type that the class creates
14
* @param P - Constructor parameter types as tuple
15
*/
16
type Class<A = any, P extends readonly any[] = any> = new (...args: P) => A;
17
```
18
19
**Usage Examples:**
20
21
```typescript
22
import { C } from "ts-toolbelt";
23
24
// Basic class type
25
type StringClass = C.Class<string, [string]>; // new (value: string) => string
26
type NumberClass = C.Class<number, [number]>; // new (value: number) => number
27
28
// Complex class constructor
29
type UserClass = C.Class<
30
{ name: string; age: number },
31
[string, number]
32
>; // new (name: string, age: number) => { name: string; age: number }
33
34
// Generic class
35
type GenericClass<T> = C.Class<T[], [T[]]>; // new (items: T[]) => T[]
36
type StringArrayClass = GenericClass<string>; // new (items: string[]) => string[]
37
```
38
39
### Instance Type Extraction
40
41
Extract the instance type that a class constructor creates.
42
43
```typescript { .api }
44
/**
45
* Extract instance type from class constructor
46
* @param C - Class constructor to extract from
47
* @returns Instance type created by the constructor
48
*/
49
type Instance<C extends Class> = C extends Class<infer I> ? I : never;
50
```
51
52
**Usage Examples:**
53
54
```typescript
55
import { C } from "ts-toolbelt";
56
57
// Built-in classes
58
type DateInstance = C.Instance<typeof Date>; // Date
59
type ArrayInstance = C.Instance<typeof Array>; // any[]
60
type MapInstance = C.Instance<typeof Map>; // Map<any, any>
61
62
// Custom classes (conceptual - would work with actual class definitions)
63
declare class User {
64
name: string;
65
age: number;
66
constructor(name: string, age: number);
67
}
68
69
type UserInstance = C.Instance<typeof User>; // User (the instance type)
70
71
// Generic classes
72
declare class Container<T> {
73
value: T;
74
constructor(value: T);
75
}
76
77
type StringContainer = C.Instance<typeof Container<string>>; // Container<string>
78
type NumberContainer = C.Instance<typeof Container<number>>; // Container<number>
79
80
// Abstract pattern
81
type SomeClass = C.Class<{ id: string; value: number }, [string, number]>;
82
type ExtractedInstance = C.Instance<SomeClass>; // { id: string; value: number }
83
```
84
85
### Constructor Parameters
86
87
Extract parameter types from class constructors.
88
89
```typescript { .api }
90
/**
91
* Extract constructor parameter types from class
92
* @param C - Class constructor to extract parameters from
93
* @returns Tuple of constructor parameter types
94
*/
95
type Parameters<C extends Class> = C extends Class<any, infer P> ? P : never;
96
```
97
98
**Usage Examples:**
99
100
```typescript
101
import { C } from "ts-toolbelt";
102
103
// Built-in class parameters
104
type DateParams = C.Parameters<typeof Date>; // [string | number | Date] | [] | [number, number, number, ...] (overloaded)
105
type ArrayParams = C.Parameters<typeof Array>; // [number] | any[] (overloaded)
106
107
// Custom class parameters
108
declare class Product {
109
constructor(name: string, price: number, inStock?: boolean);
110
}
111
112
type ProductParams = C.Parameters<typeof Product>; // [string, number, boolean?]
113
114
// No parameters
115
declare class Singleton {
116
constructor();
117
}
118
119
type SingletonParams = C.Parameters<typeof Singleton>; // []
120
121
// Rest parameters
122
declare class Collection<T> {
123
constructor(...items: T[]);
124
}
125
126
type CollectionParams = C.Parameters<typeof Collection<string>>; // [string, ...string[]]
127
128
// Complex parameter patterns
129
declare class Database {
130
constructor(
131
config: { host: string; port: number },
132
options?: { ssl: boolean; timeout: number }
133
);
134
}
135
136
type DatabaseParams = C.Parameters<typeof Database>;
137
// [{ host: string; port: number }, { ssl: boolean; timeout: number }?]
138
```
139
140
### Practical Applications
141
142
Real-world usage patterns for class type utilities.
143
144
**Usage Examples:**
145
146
```typescript
147
import { C, F } from "ts-toolbelt";
148
149
// Factory pattern
150
type Factory<T extends Class> = {
151
create: (...args: C.Parameters<T>) => C.Instance<T>;
152
createMultiple: (count: number, ...args: C.Parameters<T>) => C.Instance<T>[];
153
};
154
155
declare class User {
156
constructor(name: string, email: string);
157
}
158
159
type UserFactory = Factory<typeof User>;
160
// {
161
// create: (name: string, email: string) => User;
162
// createMultiple: (count: number, name: string, email: string) => User[];
163
// }
164
165
// Repository pattern
166
type Repository<T extends Class> = {
167
entity: T;
168
create: (...args: C.Parameters<T>) => Promise<C.Instance<T>>;
169
findById: (id: string) => Promise<C.Instance<T> | null>;
170
update: (id: string, data: Partial<C.Instance<T>>) => Promise<C.Instance<T>>;
171
delete: (id: string) => Promise<boolean>;
172
};
173
174
type UserRepository = Repository<typeof User>;
175
// Repository with User-specific types
176
177
// Dependency injection
178
type Injectable<T extends Class> = {
179
token: symbol;
180
useClass: T;
181
scope: 'singleton' | 'transient';
182
};
183
184
type Provider<T extends Class> = {
185
provide: symbol;
186
useFactory: (...deps: any[]) => C.Instance<T>;
187
inject: symbol[];
188
};
189
190
declare class ApiService {
191
constructor(httpClient: any, config: { baseUrl: string });
192
}
193
194
type ApiServiceInjectable = Injectable<typeof ApiService>;
195
type ApiServiceProvider = Provider<typeof ApiService>;
196
197
// Mixin pattern
198
type Constructor<T = {}> = C.Class<T>;
199
type Mixin<T extends Constructor> = (Base: T) => T;
200
201
declare class BaseEntity {
202
id: string;
203
constructor(id: string);
204
}
205
206
type TimestampMixin = Mixin<typeof BaseEntity>;
207
// Mixin that adds timestamp functionality
208
209
// Validation
210
type ValidateConstructor<T extends Class> = {
211
validate: (...args: C.Parameters<T>) => boolean;
212
create: (...args: C.Parameters<T>) => C.Instance<T>;
213
};
214
215
type ValidatedUser = ValidateConstructor<typeof User>;
216
// {
217
// validate: (name: string, email: string) => boolean;
218
// create: (name: string, email: string) => User;
219
// }
220
221
// Builder pattern
222
type Builder<T extends Class> = {
223
[K in keyof C.Instance<T>]: (value: C.Instance<T>[K]) => Builder<T>;
224
} & {
225
build: () => C.Instance<T>;
226
};
227
228
declare class Config {
229
host: string;
230
port: number;
231
ssl: boolean;
232
constructor(host: string, port: number, ssl: boolean);
233
}
234
235
type ConfigBuilder = Builder<typeof Config>;
236
// {
237
// host: (value: string) => ConfigBuilder;
238
// port: (value: number) => ConfigBuilder;
239
// ssl: (value: boolean) => ConfigBuilder;
240
// build: () => Config;
241
// }
242
243
// Class registry
244
type ClassRegistry = {
245
[key: string]: Class;
246
};
247
248
type RegistryInstance<R extends ClassRegistry, K extends keyof R> =
249
C.Instance<R[K]>;
250
251
type RegistryParams<R extends ClassRegistry, K extends keyof R> =
252
C.Parameters<R[K]>;
253
254
type MyRegistry = {
255
user: typeof User;
256
config: typeof Config;
257
};
258
259
type UserFromRegistry = RegistryInstance<MyRegistry, 'user'>; // User
260
type ConfigFromRegistry = RegistryInstance<MyRegistry, 'config'>; // Config
261
type UserParamsFromRegistry = RegistryParams<MyRegistry, 'user'>; // [string, string]
262
```
263
264
## Types
265
266
```typescript { .api }
267
// Core class type representing constructor functions
268
type Class<A = any, P extends readonly any[] = any> = new (...args: P) => A;
269
```