mnemonica 1.4.2 → 1.4.5
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/build/api/errors/throwModificationError.js +19 -14
- package/build/api/types/InstanceCreator.js +4 -3
- package/build/api/types/InstanceModificator.js +8 -4
- package/build/api/types/Mnemosyne.js +11 -3
- package/build/api/types/Props.d.ts +1 -0
- package/build/api/types/Props.js +8 -2
- package/build/api/types/TypeProxy.js +7 -5
- package/build/index.d.ts +9 -8
- package/build/index.js +4 -3
- package/build/types/index.d.ts +19 -19
- package/build/types/index.js +1 -1
- package/docs/prototype-chain.md +13 -1
- package/docs/typed-lookup.md +38 -0
- package/package.json +16 -6
- package/src/api/errors/throwModificationError.ts +51 -34
- package/src/api/types/InstanceCreator.ts +6 -2
- package/src/api/types/InstanceModificator.ts +16 -16
- package/src/api/types/Mnemosyne.ts +22 -2
- package/src/api/types/Props.ts +9 -0
- package/src/api/types/TypeProxy.ts +8 -4
- package/src/index.ts +67 -28
- package/src/types/index.ts +50 -34
|
@@ -19,22 +19,22 @@ export const makeInstanceModificator = ( self: InstanceCreatorContext ): Mnemoni
|
|
|
19
19
|
config,
|
|
20
20
|
} = self;
|
|
21
21
|
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
22
|
+
// Copied by descriptor, never by Object.assign, which invokes getters and
|
|
23
|
+
// freezes them to a value.
|
|
24
|
+
// Class-handler prototypes copy every member: class body methods are
|
|
25
|
+
// non-enumerable, and an enumerable-only copy silently dropped them —
|
|
26
|
+
// Type.define('X', class { m() {} }) lost m on the subtype while lazy
|
|
27
|
+
// kept it. Function-handler prototypes copy the enumerable members only:
|
|
28
|
+
// their non-enumerable props (legacy opt-in instance methods, exception
|
|
29
|
+
// wiring) were never copied, and pins depend on that (exception instances
|
|
30
|
+
// expose no bound methods)
|
|
31
|
+
const ModificatorTypePrototype: { [index: string]: unknown } = {};
|
|
32
|
+
Reflect.ownKeys( proto ).forEach( ( key ) => {
|
|
33
|
+
const descriptor = Reflect.getOwnPropertyDescriptor( proto, key );
|
|
34
|
+
if ( descriptor && ( config.asClass || descriptor.enumerable ) ) {
|
|
35
|
+
Reflect.defineProperty( ModificatorTypePrototype, key, descriptor );
|
|
36
|
+
}
|
|
37
|
+
} );
|
|
38
38
|
|
|
39
39
|
const result = ModificationConstructor.call(
|
|
40
40
|
existentInstance,
|
|
@@ -26,6 +26,12 @@ const {
|
|
|
26
26
|
|
|
27
27
|
import { InstanceCreator } from './InstanceCreator';
|
|
28
28
|
|
|
29
|
+
import { ErrorsTypes } from '../../descriptors/errors';
|
|
30
|
+
const {
|
|
31
|
+
WRONG_INSTANCE_INVOCATION,
|
|
32
|
+
WRONG_MODIFICATION_PATTERN
|
|
33
|
+
} = ErrorsTypes;
|
|
34
|
+
|
|
29
35
|
import { gatherLazyDefinition } from './lazyDefinition';
|
|
30
36
|
|
|
31
37
|
import {
|
|
@@ -103,8 +109,22 @@ const makeSubTypeProxy = function (subtype: SubtypeEntry, inheritedInstance: unk
|
|
|
103
109
|
);
|
|
104
110
|
return instanceResult;
|
|
105
111
|
}
|
|
106
|
-
|
|
107
|
-
|
|
112
|
+
// a call without `new` keeps its parent in `this`: `x.Sub()`, every
|
|
113
|
+
// link of `new Root().Sub().SubSub()`, or `.call(otherParent)`. With
|
|
114
|
+
// no `this` the subtype was detached from its instance
|
|
115
|
+
// (`const { Sub } = x; Sub()`) — that form must be `new Sub()`
|
|
116
|
+
if (this === undefined) {
|
|
117
|
+
throw new WRONG_INSTANCE_INVOCATION('detached subtype should be made with new keyword');
|
|
118
|
+
}
|
|
119
|
+
// a bare primitive parent (`x.Sub.call(5)`) is not boxed: strict
|
|
120
|
+
// mode keeps `this` primitive, and nothing can inherit from it —
|
|
121
|
+
// `null` and wrapper objects (`new Number(5)`) are handled by
|
|
122
|
+
// reflectPrimitiveWrappers, a primitive is rejected here
|
|
123
|
+
const thisType = typeof this;
|
|
124
|
+
if (this !== null && thisType !== 'object' && thisType !== 'function') {
|
|
125
|
+
throw new WRONG_MODIFICATION_PATTERN(`subtype should be made on an object, got ${thisType}`);
|
|
126
|
+
}
|
|
127
|
+
const existentInstance = reflectPrimitiveWrappers(this);
|
|
108
128
|
gatherLazyDefinition(subtype, existentInstance);
|
|
109
129
|
const instance = new InstanceCreator(
|
|
110
130
|
subtype as TypeDef,
|
package/src/api/types/Props.ts
CHANGED
|
@@ -36,6 +36,14 @@ const nativeProps = new Set([
|
|
|
36
36
|
'__self__',
|
|
37
37
|
]);
|
|
38
38
|
|
|
39
|
+
// a memory layer is a __props__ key that sits in an instance's chain: created
|
|
40
|
+
// by mnemonica per construction, so it is safe to re-link. (The props objects
|
|
41
|
+
// keyed there for setProps additions are never chain levels)
|
|
42
|
+
export const isMemoryLayer = (level: object): boolean => {
|
|
43
|
+
const result = __props__.has(level);
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
|
|
39
47
|
export const _addProps = function (this: InstanceCreatorContext): void {
|
|
40
48
|
|
|
41
49
|
|
|
@@ -291,6 +299,7 @@ export type {
|
|
|
291
299
|
};
|
|
292
300
|
|
|
293
301
|
module.exports = {
|
|
302
|
+
isMemoryLayer,
|
|
294
303
|
_addProps,
|
|
295
304
|
_getProps,
|
|
296
305
|
_setSelf,
|
|
@@ -56,10 +56,12 @@ export interface TypeProxyInstance extends TypeProxyGetHandler, TypeProxySetHand
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
export const TypeProxy = function (__type__: TypeProxyType, ancestor: unknown) {
|
|
59
|
-
|
|
59
|
+
odp(
|
|
60
60
|
this,
|
|
61
|
+
'__type__',
|
|
61
62
|
{
|
|
62
|
-
__type__
|
|
63
|
+
value : __type__,
|
|
64
|
+
enumerable : true
|
|
63
65
|
}
|
|
64
66
|
);
|
|
65
67
|
// for a lazy ROOT type the ancestor is not fixed at define time: it is
|
|
@@ -82,10 +84,12 @@ export const TypeProxy = function (__type__: TypeProxyType, ancestor: unknown) {
|
|
|
82
84
|
}
|
|
83
85
|
);
|
|
84
86
|
} else {
|
|
85
|
-
|
|
87
|
+
odp(
|
|
86
88
|
this,
|
|
89
|
+
'ancestor',
|
|
87
90
|
{
|
|
88
|
-
ancestor
|
|
91
|
+
value : ancestor,
|
|
92
|
+
enumerable : true
|
|
89
93
|
}
|
|
90
94
|
);
|
|
91
95
|
}
|
package/src/index.ts
CHANGED
|
@@ -21,13 +21,15 @@ import type {
|
|
|
21
21
|
TypeLookup,
|
|
22
22
|
RegistryHolderBase,
|
|
23
23
|
RegistryEntry,
|
|
24
|
+
GlobalRegistry,
|
|
24
25
|
LookedUpConstructor,
|
|
25
26
|
WithHookRegister,
|
|
26
27
|
typedHook,
|
|
27
28
|
HookableConstructor,
|
|
28
29
|
DefineNewableOrCallable,
|
|
29
30
|
LookupSource,
|
|
30
|
-
LazyDef
|
|
31
|
+
LazyDef,
|
|
32
|
+
ApplyFunction
|
|
31
33
|
} from './types';
|
|
32
34
|
|
|
33
35
|
import TypesUtils from './api/utils/index';
|
|
@@ -149,7 +151,7 @@ export function define <
|
|
|
149
151
|
Path extends string,
|
|
150
152
|
const Name extends string,
|
|
151
153
|
N extends object,
|
|
152
|
-
Args extends unknown[],
|
|
154
|
+
Args extends unknown[] = unknown[],
|
|
153
155
|
F extends Proto<Parent, N> = Proto<Parent, N>,
|
|
154
156
|
ChildPath extends string = Path extends '' ? Name : `${Path}.${Name}`
|
|
155
157
|
>(
|
|
@@ -162,16 +164,40 @@ export function define <
|
|
|
162
164
|
): IDefinitorInstance<
|
|
163
165
|
F,
|
|
164
166
|
InstanceResult<F>,
|
|
165
|
-
Reg & Record<ChildPath, RegistryEntry<F, ChildPath, Parent>>,
|
|
167
|
+
Reg & Record<ChildPath, RegistryEntry<F, ChildPath, Parent, Args>>,
|
|
166
168
|
ChildPath,
|
|
167
|
-
Parent
|
|
169
|
+
Parent,
|
|
170
|
+
Args
|
|
171
|
+
>;
|
|
172
|
+
export function define <
|
|
173
|
+
T extends object,
|
|
174
|
+
Args extends unknown[] = unknown[]
|
|
175
|
+
>(
|
|
176
|
+
this: unknown,
|
|
177
|
+
TypeName?: string | IDEF<T, Args>,
|
|
178
|
+
constructHandler?: IDEF<T, Args>,
|
|
179
|
+
config?: constructorOptions
|
|
180
|
+
): IDefinitorInstance<
|
|
181
|
+
T,
|
|
182
|
+
InstanceResult<T>,
|
|
183
|
+
GlobalRegistry,
|
|
184
|
+
'',
|
|
185
|
+
object,
|
|
186
|
+
Args
|
|
168
187
|
>;
|
|
169
188
|
export function define <
|
|
170
189
|
T extends object,
|
|
171
190
|
P extends object = object,
|
|
172
191
|
N extends Proto<P, T> = Proto<P, T>,
|
|
173
192
|
// here IDefinitorInstance is a CONSTRUCTOR !!!
|
|
174
|
-
R extends IDefinitorInstance<
|
|
193
|
+
R extends IDefinitorInstance<
|
|
194
|
+
N,
|
|
195
|
+
InstanceResult<N>,
|
|
196
|
+
GlobalRegistry,
|
|
197
|
+
'',
|
|
198
|
+
object,
|
|
199
|
+
never[]
|
|
200
|
+
> = IDefinitorInstance<N>,
|
|
175
201
|
>(
|
|
176
202
|
this: unknown,
|
|
177
203
|
TypeName?: string | DefineNewableOrCallable,
|
|
@@ -183,7 +209,14 @@ export function define <
|
|
|
183
209
|
T extends object,
|
|
184
210
|
P extends object = object,
|
|
185
211
|
N extends Proto<P, T> = Proto<P, T>,
|
|
186
|
-
R extends IDefinitorInstance<
|
|
212
|
+
R extends IDefinitorInstance<
|
|
213
|
+
N,
|
|
214
|
+
InstanceResult<N>,
|
|
215
|
+
GlobalRegistry,
|
|
216
|
+
'',
|
|
217
|
+
object,
|
|
218
|
+
never[]
|
|
219
|
+
> = IDefinitorInstance<N>,
|
|
187
220
|
>(
|
|
188
221
|
this: unknown,
|
|
189
222
|
TypeNameOrSource?: string | DefineNewableOrCallable | RegistryHolderBase<object, object, string>,
|
|
@@ -228,20 +261,20 @@ export function define <
|
|
|
228
261
|
return defineResult;
|
|
229
262
|
}
|
|
230
263
|
|
|
231
|
-
export function lazy<T extends object>(
|
|
264
|
+
export function lazy<T extends object, Args extends unknown[] = unknown[]>(
|
|
232
265
|
// explicit-source form: lazy(source, TypeName, getter, config?)
|
|
233
266
|
source: RegistryHolderBase<object, object, string>,
|
|
234
267
|
TypeName: string,
|
|
235
|
-
getter:
|
|
268
|
+
getter: LazyDef<T, Args>,
|
|
236
269
|
config?: constructorOptions
|
|
237
|
-
): IDefinitorInstance<T>;
|
|
238
|
-
export function lazy<T extends object>(
|
|
270
|
+
): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
|
|
271
|
+
export function lazy<T extends object, Args extends unknown[] = unknown[]>(
|
|
239
272
|
// named form: lazy(TypeName, getter, config?)
|
|
240
273
|
this: unknown,
|
|
241
274
|
TypeName: string,
|
|
242
|
-
getter: LazyDef<T>,
|
|
275
|
+
getter: LazyDef<T, Args>,
|
|
243
276
|
config?: constructorOptions
|
|
244
|
-
): IDefinitorInstance<T>;
|
|
277
|
+
): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
|
|
245
278
|
export function lazy(
|
|
246
279
|
this: unknown,
|
|
247
280
|
sourceOrTypeName: unknown,
|
|
@@ -313,9 +346,9 @@ export function lookup(
|
|
|
313
346
|
}
|
|
314
347
|
|
|
315
348
|
|
|
316
|
-
const $run = function <E extends object, T extends object, S extends Proto<E, T
|
|
349
|
+
const $run = function <E extends object, T extends object, S extends Proto<E, T>, Args extends unknown[]>(
|
|
317
350
|
entity: E,
|
|
318
|
-
Ctor: CtorParameter<T>,
|
|
351
|
+
Ctor: CtorParameter<T, Args>,
|
|
319
352
|
args: unknown[]
|
|
320
353
|
): {
|
|
321
354
|
[key in keyof S]: S[key]
|
|
@@ -340,12 +373,17 @@ const $run = function <E extends object, T extends object, S extends Proto<E, T>
|
|
|
340
373
|
};
|
|
341
374
|
|
|
342
375
|
// TODO: apply instance .to type .with arguments
|
|
343
|
-
export const apply = function <
|
|
376
|
+
export const apply: ApplyFunction = function <
|
|
377
|
+
E extends object,
|
|
378
|
+
T extends object,
|
|
379
|
+
S extends Proto<E, T>,
|
|
380
|
+
Args extends unknown[]
|
|
381
|
+
>(
|
|
344
382
|
entity: E,
|
|
345
|
-
Ctor: CtorParameter<T>,
|
|
383
|
+
Ctor: CtorParameter<T, Args>,
|
|
346
384
|
args: unknown[] = []
|
|
347
385
|
): InstanceResult<Merge<E, T>> {
|
|
348
|
-
const runResult = $run<E, T, S>(
|
|
386
|
+
const runResult = $run<E, T, S, Args>(
|
|
349
387
|
entity,
|
|
350
388
|
Ctor,
|
|
351
389
|
args
|
|
@@ -357,12 +395,12 @@ export const apply = function <E extends object, T extends object, S extends Pro
|
|
|
357
395
|
};
|
|
358
396
|
|
|
359
397
|
// TODO: call type .by instance .with arguments
|
|
360
|
-
export const call = function <E extends object, T extends object, S extends Proto<E, T
|
|
398
|
+
export const call = function <E extends object, T extends object, S extends Proto<E, T>, Args extends unknown[]>(
|
|
361
399
|
entity: E,
|
|
362
|
-
Ctor: CtorParameter<T>,
|
|
363
|
-
...args:
|
|
400
|
+
Ctor: CtorParameter<T, Args>,
|
|
401
|
+
...args: NoInfer<Args>
|
|
364
402
|
): InstanceResult<Merge<E, T>> {
|
|
365
|
-
const runResult = $run<E, T, S>(
|
|
403
|
+
const runResult = $run<E, T, S, Args>(
|
|
366
404
|
entity,
|
|
367
405
|
Ctor,
|
|
368
406
|
args
|
|
@@ -373,12 +411,12 @@ export const call = function <E extends object, T extends object, S extends Prot
|
|
|
373
411
|
};
|
|
374
412
|
|
|
375
413
|
// TODO: bind type .with instance → (...args)
|
|
376
|
-
export const bind = function <E extends object, T extends object, S extends Proto<E, T
|
|
414
|
+
export const bind = function <E extends object, T extends object, S extends Proto<E, T>, Args extends unknown[]>(
|
|
377
415
|
entity: E,
|
|
378
|
-
Ctor: CtorParameter<T>
|
|
379
|
-
): (...args:
|
|
380
|
-
const result = (...args:
|
|
381
|
-
const runResult = $run<E, T, S>(
|
|
416
|
+
Ctor: CtorParameter<T, Args>
|
|
417
|
+
): (...args: Args) => InstanceResult<Merge<E, T>> {
|
|
418
|
+
const result = (...args: Args) => {
|
|
419
|
+
const runResult = $run<E, T, S, Args>(
|
|
382
420
|
entity,
|
|
383
421
|
Ctor,
|
|
384
422
|
args
|
|
@@ -409,10 +447,11 @@ export const decorate = function <
|
|
|
409
447
|
|
|
410
448
|
const decorator = function <U extends Constructor<object>>(cstr: U): DecoratedClass<U> {
|
|
411
449
|
const { name } = cstr;
|
|
450
|
+
const handler: IDEF<object, never[]> = cstr;
|
|
412
451
|
if (parentType === undefined) {
|
|
413
452
|
const decoratorResult = define(
|
|
414
453
|
name,
|
|
415
|
-
|
|
454
|
+
handler,
|
|
416
455
|
opts
|
|
417
456
|
// decorator contract: define() returns the constructor that
|
|
418
457
|
// wraps cstr; the decorator replaces the class binding with
|
|
@@ -422,7 +461,7 @@ export const decorate = function <
|
|
|
422
461
|
}
|
|
423
462
|
const defineResult = parentType.define(
|
|
424
463
|
name,
|
|
425
|
-
|
|
464
|
+
handler,
|
|
426
465
|
opts
|
|
427
466
|
// same decorator contract as the branch above
|
|
428
467
|
) as unknown as DecoratedClass<U>;
|
package/src/types/index.ts
CHANGED
|
@@ -11,7 +11,9 @@ export type PropsType = Record<string, unknown>;
|
|
|
11
11
|
// Base constructor function type - can be a class constructor or a function.
|
|
12
12
|
// Args defaults to unknown[] so IDEF<T> is backwards compatible;
|
|
13
13
|
// supply Args to get typed constructor params: IDEF<MyType, [string, number]>
|
|
14
|
-
export type IDEF<T, Args extends unknown[] = unknown[]> =
|
|
14
|
+
export type IDEF<T, Args extends unknown[] = unknown[]> =
|
|
15
|
+
| { new(...args: Args): T }
|
|
16
|
+
| { (this: T, ...args: Args): void };
|
|
15
17
|
|
|
16
18
|
// Construction-function parameter for the .call/.apply/.bind family:
|
|
17
19
|
// anything IDEF accepts PLUS any required-argument constructor shape.
|
|
@@ -19,7 +21,7 @@ export type IDEF<T, Args extends unknown[] = unknown[]> = { new(): T } | { (this
|
|
|
19
21
|
// parameter type is a supertype of never) while keeping the constructed
|
|
20
22
|
// result typed; unknown[] would reject required params under
|
|
21
23
|
// strictFunctionTypes.
|
|
22
|
-
export type CtorParameter<T> = IDEF<T> | { new (...args:
|
|
24
|
+
export type CtorParameter<T, Args extends unknown[] = never[]> = IDEF<T, Args> | { new (...args: Args): T };
|
|
23
25
|
|
|
24
26
|
// Lazy getter type - a zero-arg factory that returns a constructor.
|
|
25
27
|
// Used by the explicit .lazy() API.
|
|
@@ -183,12 +185,12 @@ export type CreationError<T extends object> = ( T & Error ) | Error;
|
|
|
183
185
|
* TC = Type Constructor; "Internal" means this is the library's own constructor
|
|
184
186
|
* shape, distinct from user-facing TypeConstructor below.
|
|
185
187
|
*/
|
|
186
|
-
export interface _Internal_TC_<ConstructorInstance extends object> {
|
|
188
|
+
export interface _Internal_TC_<ConstructorInstance extends object, Args extends unknown[] = unknown[]> {
|
|
187
189
|
/** @throws {CreationError<ConstructorInstance>} when construction fails */
|
|
188
|
-
new(...args:
|
|
189
|
-
(this: ConstructorInstance, ...args:
|
|
190
|
+
new(...args: Args): ConstructorInstance;
|
|
191
|
+
(this: ConstructorInstance, ...args: Args): ConstructorInstance;
|
|
190
192
|
readonly prototype: ConstructorInstance & {
|
|
191
|
-
readonly constructor: _Internal_TC_<ConstructorInstance>
|
|
193
|
+
readonly constructor: _Internal_TC_<ConstructorInstance, Args>
|
|
192
194
|
};
|
|
193
195
|
}
|
|
194
196
|
|
|
@@ -620,7 +622,15 @@ export interface TypeLookup<T extends object = GlobalRegistry> extends CallableF
|
|
|
620
622
|
// interface TypeRegistry extends RegistryOf<typeof App> {}
|
|
621
623
|
// }
|
|
622
624
|
export type RegistryOf<T> =
|
|
623
|
-
|
|
625
|
+
// never[] accepts every handler Args tuple; only Registry is extracted here.
|
|
626
|
+
T extends IDefinitorInstance<
|
|
627
|
+
object,
|
|
628
|
+
InstanceResult<object>,
|
|
629
|
+
infer Registry,
|
|
630
|
+
string,
|
|
631
|
+
object,
|
|
632
|
+
never[]
|
|
633
|
+
> ? Registry
|
|
624
634
|
: T extends TypesCollection<infer Registry, object, string> ? Registry
|
|
625
635
|
: T extends MnemonicaModule<infer Registry> ? Registry
|
|
626
636
|
: never;
|
|
@@ -905,8 +915,9 @@ export type InstanceResult<
|
|
|
905
915
|
export interface RegistryEntry<
|
|
906
916
|
F extends object,
|
|
907
917
|
Path extends string = '',
|
|
908
|
-
Parent extends object = object
|
|
909
|
-
|
|
918
|
+
Parent extends object = object,
|
|
919
|
+
Args extends unknown[] = unknown[]
|
|
920
|
+
> extends _Internal_TC_<F, Args>, RegistryHolderBase<{}, F, Path> {
|
|
910
921
|
// typed hook registration — preserved through ReplaceConstructorInstance's
|
|
911
922
|
// member preservation, so lookup() results of builder-typed registries
|
|
912
923
|
// get registerHook with inferred opts
|
|
@@ -934,7 +945,7 @@ export interface RegistryHolderBase<
|
|
|
934
945
|
define<
|
|
935
946
|
const Name extends string,
|
|
936
947
|
N extends object,
|
|
937
|
-
Args extends unknown[],
|
|
948
|
+
Args extends unknown[] = unknown[],
|
|
938
949
|
F extends Proto<Parent, N> = Proto<Parent, N>,
|
|
939
950
|
ChildPath extends string = Path extends '' ? Name : `${Path}.${Name}`
|
|
940
951
|
>(
|
|
@@ -945,9 +956,10 @@ export interface RegistryHolderBase<
|
|
|
945
956
|
): IDefinitorInstance<
|
|
946
957
|
F,
|
|
947
958
|
InstanceResult<F>,
|
|
948
|
-
T & Record<ChildPath, RegistryEntry<F, ChildPath, Parent>>,
|
|
959
|
+
T & Record<ChildPath, RegistryEntry<F, ChildPath, Parent, Args>>,
|
|
949
960
|
ChildPath,
|
|
950
|
-
Parent
|
|
961
|
+
Parent,
|
|
962
|
+
Args
|
|
951
963
|
>;
|
|
952
964
|
|
|
953
965
|
// Lazy getter: .lazy(TypeName, () => Constructor, config?)
|
|
@@ -956,19 +968,21 @@ export interface RegistryHolderBase<
|
|
|
956
968
|
lazy<
|
|
957
969
|
const Name extends string,
|
|
958
970
|
N extends object,
|
|
971
|
+
Args extends unknown[],
|
|
959
972
|
F extends Proto<Parent, N> = Proto<Parent, N>,
|
|
960
973
|
ChildPath extends string = Path extends '' ? Name : `${Path}.${Name}`
|
|
961
974
|
>(
|
|
962
975
|
this: RegistryHolderBase<T, Parent, Path>,
|
|
963
976
|
TypeName: Name,
|
|
964
|
-
getter: LazyDef<N>,
|
|
977
|
+
getter: LazyDef<N, Args>,
|
|
965
978
|
config?: constructorOptions
|
|
966
979
|
): IDefinitorInstance<
|
|
967
980
|
F,
|
|
968
981
|
InstanceResult<F>,
|
|
969
|
-
T & Record<ChildPath, RegistryEntry<F, ChildPath, Parent>>,
|
|
982
|
+
T & Record<ChildPath, RegistryEntry<F, ChildPath, Parent, Args>>,
|
|
970
983
|
ChildPath,
|
|
971
|
-
Parent
|
|
984
|
+
Parent,
|
|
985
|
+
Args
|
|
972
986
|
>;
|
|
973
987
|
}
|
|
974
988
|
|
|
@@ -990,12 +1004,14 @@ export interface RegistryHolder<
|
|
|
990
1004
|
// Path = dotted type path of this constructor (empty for root types)
|
|
991
1005
|
// Parent = instance type of the parent type (object for root types);
|
|
992
1006
|
// threaded into registerHook's existentInstance typing
|
|
1007
|
+
// Args = construction parameters declared by the handler
|
|
993
1008
|
export interface IDefinitorInstance<
|
|
994
1009
|
N extends object,
|
|
995
1010
|
R extends InstanceResult<N> = InstanceResult<N>,
|
|
996
1011
|
Registry extends object = GlobalRegistry,
|
|
997
1012
|
Path extends string = '',
|
|
998
1013
|
Parent extends object = object,
|
|
1014
|
+
Args extends unknown[] = unknown[],
|
|
999
1015
|
>
|
|
1000
1016
|
extends RegistryHolderBase<Registry, N, Path> {
|
|
1001
1017
|
|
|
@@ -1007,7 +1023,7 @@ export interface IDefinitorInstance<
|
|
|
1007
1023
|
// so makes interface constructible
|
|
1008
1024
|
// new(...args: unknown[]): { [key in keyof R]: R[key] };
|
|
1009
1025
|
/** @throws {CreationError<R>} when construction fails */
|
|
1010
|
-
new(...args:
|
|
1026
|
+
new(...args: Args): R;
|
|
1011
1027
|
|
|
1012
1028
|
|
|
1013
1029
|
// TODO: need check if line below works
|
|
@@ -1039,31 +1055,31 @@ export interface IDefinitorInstance<
|
|
|
1039
1055
|
// 2. Legacy: define(constructHandler, config?) - TypeName from constructor name
|
|
1040
1056
|
// 3. Nested: parentType.define(TypeName, constructHandler, config?)
|
|
1041
1057
|
export interface TypeAbsorber extends CallableFunction {
|
|
1042
|
-
<T extends object>(
|
|
1058
|
+
<T extends object, Args extends unknown[] = unknown[]>(
|
|
1043
1059
|
this: unknown,
|
|
1044
1060
|
TypeOrTypeName: string | DefineNewableOrCallable,
|
|
1045
|
-
constructHandlerOrConfig?: IDEF<T> | object | boolean | DefineNewableOrCallable,
|
|
1061
|
+
constructHandlerOrConfig?: IDEF<T, Args> | object | boolean | DefineNewableOrCallable,
|
|
1046
1062
|
configOrUndefined?: constructorOptions | DefineNewableOrCallable | boolean
|
|
1047
|
-
): IDefinitorInstance<T>;
|
|
1063
|
+
): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
|
|
1048
1064
|
}
|
|
1049
1065
|
|
|
1050
1066
|
// Lazy absorber function type - used for explicit lazy getter definitions
|
|
1051
1067
|
// (named form only: the callback is never called at define time, so the
|
|
1052
1068
|
// name cannot be derived from it)
|
|
1053
1069
|
export interface LazyAbsorber extends CallableFunction {
|
|
1054
|
-
<T extends object>(
|
|
1070
|
+
<T extends object, Args extends unknown[] = unknown[]>(
|
|
1055
1071
|
this: unknown,
|
|
1056
1072
|
TypeName: string,
|
|
1057
|
-
getter: LazyDef<T>,
|
|
1073
|
+
getter: LazyDef<T, Args>,
|
|
1058
1074
|
config?: constructorOptions
|
|
1059
|
-
): IDefinitorInstance<T>;
|
|
1060
|
-
<T extends object>(
|
|
1075
|
+
): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
|
|
1076
|
+
<T extends object, Args extends unknown[] = unknown[]>(
|
|
1061
1077
|
this: unknown,
|
|
1062
1078
|
source: RegistryHolderBase<object, object, string>,
|
|
1063
1079
|
TypeName: string,
|
|
1064
|
-
getter: LazyDef<T>,
|
|
1080
|
+
getter: LazyDef<T, Args>,
|
|
1065
1081
|
config?: constructorOptions
|
|
1066
|
-
): IDefinitorInstance<T>;
|
|
1082
|
+
): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
|
|
1067
1083
|
}
|
|
1068
1084
|
|
|
1069
1085
|
// TypesCollection interface for createTypesCollection
|
|
@@ -1196,26 +1212,26 @@ export type Merge<E extends object, T extends object> = {
|
|
|
1196
1212
|
// fields (filtering out MnemonicaInstance method names), producing the same
|
|
1197
1213
|
// `{ fields }` hover style as `new Type()`.
|
|
1198
1214
|
export interface ApplyFunction extends CallableFunction {
|
|
1199
|
-
<E extends object, T extends object>(
|
|
1215
|
+
<E extends object, T extends object, Args extends unknown[]>(
|
|
1200
1216
|
entity: E,
|
|
1201
|
-
Constructor:
|
|
1202
|
-
args?:
|
|
1217
|
+
Constructor: CtorParameter<T, Args>,
|
|
1218
|
+
... [ args ]: [] extends Args ? [args?: NoInfer<Args>] : [args: NoInfer<Args>]
|
|
1203
1219
|
): InstanceResult<Merge<E, T>>;
|
|
1204
1220
|
}
|
|
1205
1221
|
|
|
1206
1222
|
export interface CallFunction extends CallableFunction {
|
|
1207
|
-
<E extends object, T extends object>(
|
|
1223
|
+
<E extends object, T extends object, Args extends unknown[]>(
|
|
1208
1224
|
entity: E,
|
|
1209
|
-
Constructor:
|
|
1210
|
-
...args:
|
|
1225
|
+
Constructor: CtorParameter<T, Args>,
|
|
1226
|
+
...args: NoInfer<Args>
|
|
1211
1227
|
): InstanceResult<Merge<E, T>>;
|
|
1212
1228
|
}
|
|
1213
1229
|
|
|
1214
1230
|
export interface BindFunction extends CallableFunction {
|
|
1215
|
-
<E extends object, T extends object>(
|
|
1231
|
+
<E extends object, T extends object, Args extends unknown[]>(
|
|
1216
1232
|
entity: E,
|
|
1217
|
-
Constructor:
|
|
1218
|
-
): (...args:
|
|
1233
|
+
Constructor: CtorParameter<T, Args>
|
|
1234
|
+
): (...args: Args) => InstanceResult<Merge<E, T>>;
|
|
1219
1235
|
}
|
|
1220
1236
|
|
|
1221
1237
|
// Utils object type
|