@planet-matrix/mobius-model 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +30 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +22 -4
- package/package.json +3 -3
- package/scripts/build.ts +4 -4
- package/src/basic/README.md +144 -0
- package/src/basic/array.ts +872 -0
- package/src/basic/bigint.ts +114 -0
- package/src/basic/boolean.ts +180 -0
- package/src/basic/enhance.ts +10 -0
- package/src/basic/error.ts +51 -0
- package/src/basic/function.ts +453 -0
- package/src/basic/helper.ts +276 -0
- package/src/basic/index.ts +17 -0
- package/src/basic/is.ts +320 -0
- package/src/basic/number.ts +178 -0
- package/src/basic/object.ts +140 -0
- package/src/basic/promise.ts +464 -0
- package/src/basic/regexp.ts +7 -0
- package/src/basic/stream.ts +140 -0
- package/src/basic/string.ts +308 -0
- package/src/basic/symbol.ts +164 -0
- package/src/basic/temporal.ts +224 -0
- package/src/encoding/README.md +105 -0
- package/src/encoding/base64.ts +98 -0
- package/src/encoding/index.ts +1 -0
- package/src/index.ts +4 -0
- package/src/random/README.md +109 -0
- package/src/random/index.ts +1 -0
- package/src/random/uuid.ts +103 -0
- package/src/type/README.md +330 -0
- package/src/type/array.ts +5 -0
- package/src/type/boolean.ts +471 -0
- package/src/type/class.ts +419 -0
- package/src/type/function.ts +1519 -0
- package/src/type/helper.ts +135 -0
- package/src/type/index.ts +14 -0
- package/src/type/intersection.ts +93 -0
- package/src/type/is.ts +247 -0
- package/src/type/iteration.ts +233 -0
- package/src/type/number.ts +732 -0
- package/src/type/object.ts +788 -0
- package/src/type/path.ts +73 -0
- package/src/type/string.ts +1004 -0
- package/src/type/tuple.ts +2424 -0
- package/src/type/union.ts +108 -0
- package/tests/unit/basic/array.spec.ts +290 -0
- package/tests/unit/basic/bigint.spec.ts +50 -0
- package/tests/unit/basic/boolean.spec.ts +74 -0
- package/tests/unit/basic/error.spec.ts +32 -0
- package/tests/unit/basic/function.spec.ts +175 -0
- package/tests/unit/basic/helper.spec.ts +118 -0
- package/tests/unit/basic/number.spec.ts +74 -0
- package/tests/unit/basic/object.spec.ts +46 -0
- package/tests/unit/basic/promise.spec.ts +232 -0
- package/tests/unit/basic/regexp.spec.ts +11 -0
- package/tests/unit/basic/stream.spec.ts +120 -0
- package/tests/unit/basic/string.spec.ts +74 -0
- package/tests/unit/basic/symbol.spec.ts +72 -0
- package/tests/unit/basic/temporal.spec.ts +78 -0
- package/tests/unit/encoding/base64.spec.ts +40 -0
- package/tests/unit/random/uuid.spec.ts +37 -0
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/reactor/index.d.ts +0 -3
- package/dist/reactor/index.d.ts.map +0 -1
- package/dist/reactor/reactor-core/flags.d.ts +0 -99
- package/dist/reactor/reactor-core/flags.d.ts.map +0 -1
- package/dist/reactor/reactor-core/index.d.ts +0 -4
- package/dist/reactor/reactor-core/index.d.ts.map +0 -1
- package/dist/reactor/reactor-core/primitive.d.ts +0 -276
- package/dist/reactor/reactor-core/primitive.d.ts.map +0 -1
- package/dist/reactor/reactor-core/reactive-system.d.ts +0 -241
- package/dist/reactor/reactor-core/reactive-system.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/branch.d.ts +0 -19
- package/dist/reactor/reactor-operators/branch.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/convert.d.ts +0 -30
- package/dist/reactor/reactor-operators/convert.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/create.d.ts +0 -26
- package/dist/reactor/reactor-operators/create.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/filter.d.ts +0 -269
- package/dist/reactor/reactor-operators/filter.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/index.d.ts +0 -8
- package/dist/reactor/reactor-operators/index.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/join.d.ts +0 -48
- package/dist/reactor/reactor-operators/join.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/map.d.ts +0 -165
- package/dist/reactor/reactor-operators/map.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/utility.d.ts +0 -48
- package/dist/reactor/reactor-operators/utility.d.ts.map +0 -1
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
// oxlint-disable no-explicit-any
|
|
2
|
+
|
|
3
|
+
// ============================================================================
|
|
4
|
+
// Primitives
|
|
5
|
+
// ============================================================================
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Define a constructor type for a class.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```
|
|
12
|
+
* // Expect: new (name: string, age: number) => Person
|
|
13
|
+
* type Example1 = ClassConstructor<Person, [name: string, age: number]>
|
|
14
|
+
* // Expect: new () => Date
|
|
15
|
+
* type Example2 = ClassConstructor<Date>
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export type ClassConstructor<Instance = unknown, Args extends readonly unknown[] = []> =
|
|
19
|
+
new (...args: Args) => Instance
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Define an abstract constructor type for a class.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```
|
|
26
|
+
* abstract class Base {}
|
|
27
|
+
* // Expect: abstract new () => Base
|
|
28
|
+
* type Example1 = ClassAbstractConstructor<Base>
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export type ClassAbstractConstructor<Instance = unknown, Args extends readonly unknown[] = []> =
|
|
32
|
+
abstract new (...args: Args) => Instance
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Define a class decorator signature.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```
|
|
39
|
+
* class Base {}
|
|
40
|
+
* // Expect: (target: typeof Base) => typeof Base
|
|
41
|
+
* type Example1 = ClassDecorator<typeof Base>
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export type ClassDecorator<C extends abstract new (...args: any[]) => any> =
|
|
45
|
+
(target: C) => C
|
|
46
|
+
|
|
47
|
+
// ============================================================================
|
|
48
|
+
// Validation
|
|
49
|
+
// ============================================================================
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Check if a type is a constructor.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```
|
|
56
|
+
* class Foo {}
|
|
57
|
+
* type Factory = () => Foo
|
|
58
|
+
* // Expect: true
|
|
59
|
+
* type Example1 = ClassIsConstructor<typeof Foo>
|
|
60
|
+
* // Expect: false
|
|
61
|
+
* type Example2 = ClassIsConstructor<Factory>
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export type ClassIsConstructor<T> = T extends new (...args: any[]) => any ? true : false
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Check if a type is an abstract constructor.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```
|
|
71
|
+
* abstract class Base {}
|
|
72
|
+
* class Concrete {}
|
|
73
|
+
* // Expect: true
|
|
74
|
+
* type Example1 = ClassIsAbstractConstructor<typeof Base>
|
|
75
|
+
* // Expect: false
|
|
76
|
+
* type Example2 = ClassIsAbstractConstructor<typeof Concrete>
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export type ClassIsAbstractConstructor<T> = T extends abstract new (...args: any[]) => any ? true : false
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Check if a type is a concrete (non-abstract) constructor.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```
|
|
86
|
+
* abstract class Base {}
|
|
87
|
+
* class Concrete {}
|
|
88
|
+
* type Factory = () => Concrete
|
|
89
|
+
* // Expect: true
|
|
90
|
+
* type Example1 = ClassIsConcreteConstructor<typeof Concrete>
|
|
91
|
+
* // Expect: false
|
|
92
|
+
* type Example2 = ClassIsConcreteConstructor<typeof Base>
|
|
93
|
+
* // Expect: false
|
|
94
|
+
* type Example3 = ClassIsConcreteConstructor<Factory>
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export type ClassIsConcreteConstructor<T> =
|
|
98
|
+
T extends new (...args: any[]) => any
|
|
99
|
+
? (T extends abstract new (...args: any[]) => any ? false : true)
|
|
100
|
+
: false
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Check if a constructor has parameters.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```
|
|
107
|
+
* class Empty { constructor() {} }
|
|
108
|
+
* class Person { constructor(name: string) {} }
|
|
109
|
+
* // Expect: false
|
|
110
|
+
* type Example1 = ClassHasConstructorParameters<typeof Empty>
|
|
111
|
+
* // Expect: true
|
|
112
|
+
* type Example2 = ClassHasConstructorParameters<typeof Person>
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export type ClassHasConstructorParameters<C extends abstract new (...args: any[]) => any> =
|
|
116
|
+
ConstructorParameters<C> extends infer P extends readonly unknown[]
|
|
117
|
+
? (P['length'] extends 0 ? false : true)
|
|
118
|
+
: false
|
|
119
|
+
|
|
120
|
+
// ============================================================================
|
|
121
|
+
// Comparison
|
|
122
|
+
// ============================================================================
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Check if a value type is an instance of a class constructor.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```
|
|
129
|
+
* class Person { name = 'demo' }
|
|
130
|
+
* type Value = { name: string }
|
|
131
|
+
* // Expect: true
|
|
132
|
+
* type Example1 = ClassIsInstance<Value, typeof Person>
|
|
133
|
+
* // Expect: false
|
|
134
|
+
* type Example2 = ClassIsInstance<number, typeof Person>
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
export type ClassIsInstance<Value, C extends abstract new (...args: any[]) => any> =
|
|
138
|
+
Value extends InstanceType<C> ? true : false
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Check if class A extends class B (structurally by instance type).
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```
|
|
145
|
+
* class Base { id = 1 }
|
|
146
|
+
* class Derived extends Base {}
|
|
147
|
+
* // Expect: true
|
|
148
|
+
* type Example1 = ClassIsExtend<typeof Derived, typeof Base>
|
|
149
|
+
* // Expect: false
|
|
150
|
+
* type Example2 = ClassIsExtend<typeof Base, typeof Derived>
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
export type ClassIsExtend<A extends abstract new (...args: any[]) => any, B extends abstract new (...args: any[]) => any> =
|
|
154
|
+
InstanceType<A> extends InstanceType<B> ? true : false
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Check if class C implements interface I (structurally by instance type).
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```
|
|
161
|
+
* interface Shape { area(): number }
|
|
162
|
+
* class Square implements Shape { area() { return 1 } }
|
|
163
|
+
* // Expect: true
|
|
164
|
+
* type Example1 = ClassIsImplement<typeof Square, Shape>
|
|
165
|
+
* // Expect: false
|
|
166
|
+
* type Example2 = ClassIsImplement<typeof Square, { perimeter(): number }>
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
export type ClassIsImplement<C extends abstract new (...args: any[]) => any, I> =
|
|
170
|
+
InstanceType<C> extends I ? true : false
|
|
171
|
+
|
|
172
|
+
// ============================================================================
|
|
173
|
+
// Query
|
|
174
|
+
// ============================================================================
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Get constructor parameters of a class.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```
|
|
181
|
+
* class Person { constructor(name: string, age: number) {} }
|
|
182
|
+
* class Empty { constructor() {} }
|
|
183
|
+
* // Expect: [name: string, age: number]
|
|
184
|
+
* type Example1 = ClassConstructorParameters<typeof Person>
|
|
185
|
+
* // Expect: []
|
|
186
|
+
* type Example2 = ClassConstructorParameters<typeof Empty>
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
export type ClassConstructorParameters<C extends abstract new (...args: any[]) => any> =
|
|
190
|
+
ConstructorParameters<C>
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Get the constructor parameter at index N.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```
|
|
197
|
+
* class Person { constructor(name: string, age: number) {} }
|
|
198
|
+
* // Expect: string
|
|
199
|
+
* type Example1 = ClassConstructorParameterAt<typeof Person, 0>
|
|
200
|
+
* // Expect: number
|
|
201
|
+
* type Example2 = ClassConstructorParameterAt<typeof Person, 1>
|
|
202
|
+
* // Expect: never
|
|
203
|
+
* type Example3 = ClassConstructorParameterAt<typeof Person, 2>
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
export type ClassConstructorParameterAt<
|
|
207
|
+
C extends abstract new (...args: any[]) => any,
|
|
208
|
+
Index extends number,
|
|
209
|
+
> = ConstructorParameters<C> extends infer P extends readonly unknown[]
|
|
210
|
+
? (Index extends keyof P ? P[Index] : never)
|
|
211
|
+
: never
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Get constructor arity (parameter count).
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```
|
|
218
|
+
* class Person { constructor(name: string, age: number) {} }
|
|
219
|
+
* class Empty { constructor() {} }
|
|
220
|
+
* // Expect: 2
|
|
221
|
+
* type Example1 = ClassConstructorArity<typeof Person>
|
|
222
|
+
* // Expect: 0
|
|
223
|
+
* type Example2 = ClassConstructorArity<typeof Empty>
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
export type ClassConstructorArity<C extends abstract new (...args: any[]) => any> =
|
|
227
|
+
ConstructorParameters<C>['length']
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Get instance type from a class constructor.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```
|
|
234
|
+
* class Person { name = 'demo' }
|
|
235
|
+
* // Expect: Person
|
|
236
|
+
* type Example1 = ClassInstanceType<typeof Person>
|
|
237
|
+
* ```
|
|
238
|
+
*/
|
|
239
|
+
export type ClassInstanceType<C extends abstract new (...args: any[]) => any> =
|
|
240
|
+
InstanceType<C>
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Get instance type from an abstract constructor.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* ```
|
|
247
|
+
* abstract class Base { id = 1 }
|
|
248
|
+
* // Expect: Base
|
|
249
|
+
* type Example1 = ClassAbstractInstanceType<typeof Base>
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
export type ClassAbstractInstanceType<C extends abstract new (...args: any[]) => any> =
|
|
253
|
+
InstanceType<C>
|
|
254
|
+
|
|
255
|
+
type InternalInstanceRecord<C extends abstract new (...args: any[]) => any> =
|
|
256
|
+
InstanceType<C> extends infer I ? { [K in keyof I]: I[K] } : never
|
|
257
|
+
/**
|
|
258
|
+
* Get all public keys on a class instance.
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* ```
|
|
262
|
+
* class Person { name = 'demo'; age = 1; greet() {} }
|
|
263
|
+
* // Expect: 'name' | 'age' | 'greet'
|
|
264
|
+
* type Example1 = ClassPublicKeys<typeof Person>
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
export type ClassPublicKeys<C extends abstract new (...args: any[]) => any> =
|
|
268
|
+
keyof InternalInstanceRecord<C>
|
|
269
|
+
|
|
270
|
+
type InternalFunctionKeys<T extends Record<PropertyKey, unknown>> = {
|
|
271
|
+
[K in keyof T]-?: T[K] extends (...args: any[]) => any ? K : never
|
|
272
|
+
}[keyof T]
|
|
273
|
+
/**
|
|
274
|
+
* Get method keys from a class instance.
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```
|
|
278
|
+
* class Person { name = 'demo'; greet() {} }
|
|
279
|
+
* // Expect: 'greet'
|
|
280
|
+
* type Example1 = ClassMethodKeys<typeof Person>
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
export type ClassMethodKeys<C extends abstract new (...args: any[]) => any> =
|
|
284
|
+
InternalFunctionKeys<InternalInstanceRecord<C>>
|
|
285
|
+
|
|
286
|
+
type InternalNonFunctionKeys<T extends Record<PropertyKey, unknown>> = {
|
|
287
|
+
[K in keyof T]-?: T[K] extends (...args: any[]) => any ? never : K
|
|
288
|
+
}[keyof T]
|
|
289
|
+
/**
|
|
290
|
+
* Get field keys (non-function) from a class instance.
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* ```
|
|
294
|
+
* class Person { name = 'demo'; greet() {} }
|
|
295
|
+
* // Expect: 'name'
|
|
296
|
+
* type Example1 = ClassFieldKeys<typeof Person>
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
export type ClassFieldKeys<C extends abstract new (...args: any[]) => any> =
|
|
300
|
+
InternalNonFunctionKeys<InternalInstanceRecord<C>>
|
|
301
|
+
|
|
302
|
+
type InternalIfEquals<X, Y, TRUE = X, FALSE = never> =
|
|
303
|
+
(<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? TRUE : FALSE
|
|
304
|
+
type InternalReadonlyKeys<T extends Record<PropertyKey, unknown>> = {
|
|
305
|
+
[K in keyof T]-?: InternalIfEquals<{ [Q in K]: T[K] }, { -readonly [Q in K]: T[K] }, never, K>
|
|
306
|
+
}[keyof T]
|
|
307
|
+
/**
|
|
308
|
+
* Get readonly keys from a class instance.
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* ```
|
|
312
|
+
* class Person { readonly id = 1; name = 'demo' }
|
|
313
|
+
* // Expect: 'id'
|
|
314
|
+
* type Example1 = ClassReadonlyKeys<typeof Person>
|
|
315
|
+
* ```
|
|
316
|
+
*/
|
|
317
|
+
export type ClassReadonlyKeys<C extends abstract new (...args: any[]) => any> =
|
|
318
|
+
InternalReadonlyKeys<InternalInstanceRecord<C>>
|
|
319
|
+
|
|
320
|
+
type InternalWritableKeys<T extends Record<PropertyKey, unknown>> = {
|
|
321
|
+
[K in keyof T]-?: InternalIfEquals<{ [Q in K]: T[K] }, { -readonly [Q in K]: T[K] }, K, never>
|
|
322
|
+
}[keyof T]
|
|
323
|
+
/**
|
|
324
|
+
* Get writable keys from a class instance.
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```
|
|
328
|
+
* class Person { readonly id = 1; name = 'demo' }
|
|
329
|
+
* // Expect: 'name'
|
|
330
|
+
* type Example1 = ClassWritableKeys<typeof Person>
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
333
|
+
export type ClassWritableKeys<C extends abstract new (...args: any[]) => any> =
|
|
334
|
+
InternalWritableKeys<InternalInstanceRecord<C>>
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Get static keys from a class constructor.
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* ```
|
|
341
|
+
* class Person { static role = 'user'; name = 'demo' }
|
|
342
|
+
* // Expect: 'prototype' | 'role'
|
|
343
|
+
* type Example1 = ClassStaticKeys<typeof Person>
|
|
344
|
+
* ```
|
|
345
|
+
*/
|
|
346
|
+
export type ClassStaticKeys<C extends abstract new (...args: any[]) => any> =
|
|
347
|
+
keyof C
|
|
348
|
+
|
|
349
|
+
// ============================================================================
|
|
350
|
+
// Extraction
|
|
351
|
+
// ============================================================================
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Pick static members from a class constructor.
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* ```
|
|
358
|
+
* class Person { static role = 'user'; static version = 1; name = 'demo' }
|
|
359
|
+
* // Expect: { role: string }
|
|
360
|
+
* type Example1 = ClassStaticPick<typeof Person, 'role'>
|
|
361
|
+
* ```
|
|
362
|
+
*/
|
|
363
|
+
export type ClassStaticPick<
|
|
364
|
+
C extends abstract new (...args: any[]) => any,
|
|
365
|
+
K extends keyof C,
|
|
366
|
+
> = Pick<C, K>
|
|
367
|
+
|
|
368
|
+
// ============================================================================
|
|
369
|
+
// Manipulation
|
|
370
|
+
// ============================================================================
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Apply a mixin to a class constructor.
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* ```
|
|
377
|
+
* class Base { base = true }
|
|
378
|
+
* type Mixin = (base: typeof Base) => abstract new (...args: any[]) => Base & { extra: string }
|
|
379
|
+
* // Expect: abstract new (...args: any[]) => Base & { extra: string }
|
|
380
|
+
* type Example1 = ClassWithMixin<typeof Base, Mixin>
|
|
381
|
+
* ```
|
|
382
|
+
*/
|
|
383
|
+
export type ClassWithMixin<
|
|
384
|
+
C extends abstract new (...args: any[]) => any,
|
|
385
|
+
M extends (base: C) => abstract new (...args: any[]) => any,
|
|
386
|
+
> = M extends (base: C) => infer Result ? Result : never
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Apply a decorator to a class constructor.
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* ```
|
|
393
|
+
* class Base {}
|
|
394
|
+
* type Decorator = (target: typeof Base) => typeof Base
|
|
395
|
+
* // Expect: typeof Base
|
|
396
|
+
* type Example1 = ClassDecorate<typeof Base, Decorator>
|
|
397
|
+
* ```
|
|
398
|
+
*/
|
|
399
|
+
export type ClassDecorate<
|
|
400
|
+
C extends abstract new (...args: any[]) => any,
|
|
401
|
+
D extends (target: C) => C,
|
|
402
|
+
> = D extends (target: C) => infer Result ? Result : never
|
|
403
|
+
|
|
404
|
+
// ============================================================================
|
|
405
|
+
// Conversion
|
|
406
|
+
// ============================================================================
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Convert a class constructor to a regular function signature.
|
|
410
|
+
*
|
|
411
|
+
* @example
|
|
412
|
+
* ```
|
|
413
|
+
* class Person { constructor(name: string, age: number) {} }
|
|
414
|
+
* // Expect: (name: string, age: number) => Person
|
|
415
|
+
* type Example1 = ClassConstructorToFunction<typeof Person>
|
|
416
|
+
* ```
|
|
417
|
+
*/
|
|
418
|
+
export type ClassConstructorToFunction<C extends abstract new (...args: any[]) => any> =
|
|
419
|
+
(...args: ConstructorParameters<C>) => InstanceType<C>
|