mnemonica 1.4.9 → 1.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.
@@ -15,13 +15,22 @@ export type IDEF<T, Args extends unknown[] = unknown[]> =
15
15
  | { new(...args: Args): T }
16
16
  | { (this: T, ...args: Args): void };
17
17
 
18
- // Construction-function parameter for the .call/.apply/.bind family:
19
- // anything IDEF accepts PLUS any required-argument constructor shape.
18
+ // An async handler: a function returning a Promise, or a declared constructor
19
+ // of one. TypeScript refuses to write such a class (TS2409), so the second
20
+ // comes from a declaration.
21
+ export type AsyncIDEF<T, Args extends unknown[] = unknown[]> =
22
+ | { (this: T, ...args: Args): PromiseLike<unknown> }
23
+ | { new (...args: Args): PromiseLike<T> };
24
+
25
+ // Construction-function parameter for the .call/.apply/.bind family: a
26
+ // constructor, never the plain function a user gives define(). TypeName is
27
+ // not required here: a decorated class is a mnemonica constructor at
28
+ // runtime, and its binding keeps the type of the class as written.
20
29
  // The never[] rest accepts every parameter list by contravariance (every
21
30
  // parameter type is a supertype of never) while keeping the constructed
22
31
  // result typed; unknown[] would reject required params under
23
32
  // strictFunctionTypes.
24
- export type CtorParameter<T, Args extends unknown[] = never[]> = IDEF<T, Args> | { new (...args: Args): T };
33
+ export type CtorParameter<T, Args extends unknown[] = never[]> = { new (...args: Args): T };
25
34
 
26
35
  // Lazy getter type - a zero-arg factory that returns a constructor.
27
36
  // Used by the explicit .lazy() API.
@@ -220,6 +229,9 @@ export interface FlowChecker extends CallableFunction {
220
229
  // Hook types
221
230
  export type hooksTypes = 'preCreation' | 'postCreation' | 'creationError';
222
231
 
232
+ // What the registering methods take: a hook type or its short name
233
+ export type hookName = hooksTypes | 'pre' | 'post' | 'error';
234
+
223
235
  // Hook options passed to hook callbacks
224
236
  // P = parent / existent instance (proto)
225
237
  // T = child / inherited instance (type being created)
@@ -264,8 +276,8 @@ export type deliveredHookOpts<P = object, T = P> = {
264
276
  // preCreation fires BEFORE the created instance exists, so its opts carry
265
277
  // no inheritedInstance and no throwModificationError — accessing them is a
266
278
  // compile error.
267
- export type typedHookOpts<HT extends hooksTypes, P extends object, T extends object> =
268
- HT extends 'preCreation'
279
+ export type typedHookOpts<HT extends hookName, P extends object, T extends object> =
280
+ HT extends 'preCreation' | 'pre'
269
281
  ? {
270
282
  TypeName: string;
271
283
  type: TypeDef;
@@ -289,33 +301,44 @@ export type typedHookOpts<HT extends hooksTypes, P extends object, T extends obj
289
301
  // a concrete instance is the callback's business (instanceof lookup('User'))
290
302
  // — but the preCreation/post payload split holds at both levels.
291
303
  export interface typedHook<
292
- HT extends hooksTypes,
304
+ HT extends hookName,
293
305
  P extends object = object,
294
306
  T extends object = object
295
307
  > extends CallableFunction {
296
308
  (opts: typedHookOpts<HT, P, T>): unknown;
297
309
  }
298
310
 
311
+ // The registering methods of a type (P = its parent instance, T = its own) or
312
+ // a collection (both stay `object`). on / off are registerHook / unregisterHook
313
+ // under short names. unregisterHook takes any callback: it only looks up the
314
+ // exact reference, and returns whether it was registered.
315
+ export interface HookMethods<P extends object = object, T extends object = object> {
316
+ registerHook<HT extends hookName>(hookType: HT, cb: typedHook<HT, P, T>): void;
317
+ unregisterHook(hookType: hookName, cb: AnyHookCallback): boolean;
318
+ on<HT extends hookName>(hookType: HT, cb: typedHook<HT, P, T>): void;
319
+ off(hookType: hookName, cb: AnyHookCallback): boolean;
320
+ }
321
+
299
322
  // Constructor surface the free registerHook() accepts: any constructor of T
300
323
  // instances with a registerHook method. Deliberately NOT DecoratedClass<T> —
301
324
  // inference through DecoratedClass's InstanceType<T> conditional picks a wrong
302
325
  // T candidate, and inferring from a generic registerHook member resets T to
303
326
  // its default; here T is inferred from Constructor<T>'s return position and
304
327
  // the precise cb typing comes from the free function's own cb parameter.
305
- export type HookableConstructor<T extends object> = Constructor<T> & {
306
- registerHook(hookType: hooksTypes, cb: AnyHookCallback): void;
328
+ // An async constructor's `new` gives Promise<T>: hooks see the resolved T.
329
+ export type HookableConstructor<T extends object> = Constructor<T | PromiseLike<T>> & {
330
+ registerHook(hookType: hookName, cb: AnyHookCallback): void;
307
331
  };
308
332
 
309
- // Shared interface for objects that support hooks (TypeDef and CollectionDef)
310
- export interface Hookable {
333
+ // Shared interface for objects that support hooks (TypeDef and CollectionDef).
334
+ // Collection-level registration: the payload split by hookType holds
335
+ // (preCreation has no inheritedInstance/throwModificationError; post
336
+ // hooks carry both, required) but P/T stay `object` — a collection
337
+ // mixes every registered type, so narrowing to a concrete instance
338
+ // happens in the callback.
339
+ export interface Hookable extends HookMethods {
311
340
  hooks: Record<string, Set<hook>>;
312
341
  invokeHook(hookType: hooksTypes, opts: hooksOpts): Set<unknown>;
313
- // Collection-level registration: the payload split by hookType holds
314
- // (preCreation has no inheritedInstance/throwModificationError; post
315
- // hooks carry both, required) but P/T stay `object` — a collection
316
- // mixes every registered type, so narrowing to a concrete instance
317
- // happens in the callback.
318
- registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT>): void;
319
342
  registerFlowChecker(cb: FlowChecker): void;
320
343
  }
321
344
 
@@ -374,8 +397,23 @@ export type constructorOptions = {
374
397
  // with true a non-object resolution DROPS the chain and that value is
375
398
  // the result ("unchain on await")
376
399
  unchain?: boolean,
377
- // Force class mode (auto-detected by default)
378
- asClass?: boolean,
400
+ };
401
+
402
+ // What define(), lazy() and decorate() take in the config place: the options,
403
+ // strictChain alone, or ModificationConstructor alone.
404
+ export type DefineConfig = constructorOptions | boolean | ModificationConstructorFactory;
405
+
406
+ // The same where a function means something else: the handler of define(),
407
+ // the parent type of decorate().
408
+ export type NoHandlerConfig = constructorOptions | boolean;
409
+
410
+ // The config a type keeps for itself: the given options merged over the
411
+ // collection's, plus what define() detected from the handler. asClass is
412
+ // never an option: a class body or a plain function is a property of the
413
+ // handler, detected by isClass at every define and, for a lazy type, at
414
+ // every construction.
415
+ export type TypeConfig = constructorOptions & {
416
+ asClass: boolean,
379
417
  };
380
418
 
381
419
  // Subtypes map - represents the subtypes property
@@ -388,7 +426,7 @@ export type TypeDef = {
388
426
  isSubType: boolean;
389
427
  subtypes: SubtypesMap;
390
428
  collection: CollectionDef;
391
- config: constructorOptions;
429
+ config: TypeConfig;
392
430
  parentType?: TypeDef;
393
431
  constructHandler: MnemonicaConstructorFactory;
394
432
  title: string;
@@ -444,7 +482,7 @@ export interface InstanceCreatorContext {
444
482
  ModificatorType: MnemonicaConstructor;
445
483
  InstanceModificator: MnemonicaConstructor;
446
484
  inheritedInstance: object | Promise<object>;
447
- config: constructorOptions;
485
+ config: TypeConfig;
448
486
  proto: object;
449
487
  __proto_proto__?: object;
450
488
  stack?: string[];
@@ -502,8 +540,8 @@ export interface MnemonicaConstructor extends NewableFunction {
502
540
  export interface TypeDescriptorDefine extends CallableFunction {
503
541
  (
504
542
  TypeOrTypeName: string | DefineNewableOrCallable,
505
- constructHandlerOrConfig?: DefineNewableOrCallable | object,
506
- config?: object
543
+ constructHandlerOrConfig?: DefineNewableOrCallable | object | boolean,
544
+ config?: object | boolean
507
545
  ): TypeClass;
508
546
  }
509
547
 
@@ -511,7 +549,7 @@ export interface TypeDescriptorLazy extends CallableFunction {
511
549
  (
512
550
  TypeName: string,
513
551
  getter: LazyDef<object>,
514
- config?: object
552
+ config?: object | boolean
515
553
  ): TypeClass;
516
554
  }
517
555
 
@@ -522,7 +560,7 @@ export interface TypeDescriptorLookup extends CallableFunction {
522
560
  export type TypeDescriptorInstance = {
523
561
  define: TypeDescriptorDefine;
524
562
  lazy: TypeDescriptorLazy;
525
- decorate: (config?: constructorOptions) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U>;
563
+ decorate: (config?: DefineConfig) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U>;
526
564
  lookup: TypeDescriptorLookup;
527
565
  subtypes: Map<string, object>;
528
566
  TypeName: string;
@@ -734,12 +772,12 @@ export type LookedUpConstructor<
734
772
  export type WithHookRegister<C> = C extends { registerHook: AnyHookCallback }
735
773
  ? C
736
774
  : C extends { new (...args: never[]): infer R }
737
- ? R extends object
738
- ? C & { registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, object, R>): void }
775
+ ? Awaited<R> extends infer A extends object
776
+ ? C & HookMethods<object, A>
739
777
  : C
740
778
  : C extends { (...args: never[]): infer R }
741
779
  ? R extends object
742
- ? C & { registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, object, R>): void }
780
+ ? C & HookMethods<object, R>
743
781
  : C
744
782
  : C;
745
783
 
@@ -783,7 +821,8 @@ export type RegistryOf<T> =
783
821
  infer Registry,
784
822
  string,
785
823
  object,
786
- never[]
824
+ never[],
825
+ object
787
826
  > ? Registry
788
827
  : T extends TypesCollection<infer Registry, object, string> ? Registry
789
828
  : T extends MnemonicaModule<infer Registry> ? Registry
@@ -835,12 +874,11 @@ export interface RegistryEntry<
835
874
  Path extends string = '',
836
875
  Parent extends object = object,
837
876
  Args extends unknown[] = unknown[]
838
- > extends _Internal_TC_<F, Args>, RegistryHolderBase<{}, F, Path> {
877
+ >
839
878
  // typed hook registration — preserved through ReplaceConstructorInstance's
840
879
  // member preservation, so lookup() results of builder-typed registries
841
880
  // get registerHook with inferred opts
842
- registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, Parent, F>): void;
843
- }
881
+ extends _Internal_TC_<F, Args>, RegistryHolderBase<{}, F, Path>, HookMethods<Parent, F> {}
844
882
 
845
883
  // Async registry VALUE: the same public vocabulary as RegistryEntry, with
846
884
  // construction returning the Promise that the runtime always produces for an
@@ -851,13 +889,12 @@ export interface AsyncRegistryEntry<
851
889
  Path extends string = '',
852
890
  Parent extends object = object,
853
891
  Args extends unknown[] = unknown[]
854
- > extends RegistryHolderBase<{}, F, Path> {
892
+ > extends RegistryHolderBase<{}, F, Path>, HookMethods<Parent, F> {
855
893
  new (...args: Args): Promise<F>;
856
894
  (this: F, ...args: Args): Promise<F>;
857
895
  readonly prototype: F & {
858
896
  readonly constructor: AsyncRegistryEntry<F, Path, Parent, Args>;
859
897
  };
860
- registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, Parent, F>): void;
861
898
  }
862
899
 
863
900
  // The dotted path of a type named `Name` defined on a holder at `Path`:
@@ -876,17 +913,87 @@ export interface RegistryHolderBase<
876
913
  Parent extends object = object,
877
914
  Path extends string = ''
878
915
  > {
879
- // Legacy overload: define(constructHandler, config?)
880
- define<SubType extends object>(
916
+ // define() has three forms, the same on every level, and the config is
917
+ // always the last argument:
918
+ // define(constructHandler, config?) TypeName is its own name
919
+ // define(TypeName, constructHandler, config?)
920
+ // define(TypeName, config?) no handler
921
+ //
922
+ // In the first form TypeScript sees the handler's name only as `string`,
923
+ // so nothing is added to the registry: Path is never, and so is the path
924
+ // of every type defined on the result.
925
+ //
926
+ // The order inside a form: a `=> never` handler (one that only throws) goes
927
+ // before the async one, whose Promise it would match, and so would an `any`
928
+ // handler; the async one goes before IDEF, whose callable void result
929
+ // otherwise accepts async functions and erases their Promise.
930
+ define<
931
+ N extends object,
932
+ Args extends unknown[] = unknown[],
933
+ F extends Proto<Parent, N> = Proto<Parent, N>
934
+ >(
935
+ this: RegistryHolderBase<T, Parent, Path>,
936
+ constructHandler: (this: N, ...args: Args) => never,
937
+ config?: DefineConfig
938
+ ): IDefinitorInstance<
939
+ F,
940
+ InstanceResult<F>,
941
+ T,
942
+ never,
943
+ Parent,
944
+ Args
945
+ >;
946
+ define<
947
+ N extends object,
948
+ Args extends unknown[] = unknown[],
949
+ F extends Proto<Parent, N> = Proto<Parent, N>
950
+ >(
951
+ this: RegistryHolderBase<T, Parent, Path>,
952
+ constructHandler: AsyncIDEF<N, Args>,
953
+ config?: DefineConfig
954
+ ): IDefinitorInstance<
955
+ F,
956
+ InstanceResult<F>,
957
+ T,
958
+ never,
959
+ Parent,
960
+ Args,
961
+ Promise<InstanceResult<F>>
962
+ >;
963
+ define<
964
+ N extends object,
965
+ Args extends unknown[] = unknown[],
966
+ F extends Proto<Parent, N> = Proto<Parent, N>
967
+ >(
968
+ this: RegistryHolderBase<T, Parent, Path>,
969
+ constructHandler: IDEF<N, Args>,
970
+ config?: DefineConfig
971
+ ): IDefinitorInstance<
972
+ F,
973
+ InstanceResult<F>,
974
+ T,
975
+ never,
976
+ Parent,
977
+ Args
978
+ >;
979
+ // the instance type given explicitly: define<Shape>(constructHandler)
980
+ define<
981
+ N extends object,
982
+ F extends Proto<Parent, N> = Proto<Parent, N>
983
+ >(
881
984
  this: RegistryHolderBase<T, Parent, Path>,
882
- TypeOrTypeName: DefineNewableOrCallable,
883
- constructHandlerOrConfig?: IDEF<SubType> | object | boolean | DefineNewableOrCallable,
884
- configOrUndefined?: constructorOptions | DefineNewableOrCallable | boolean
885
- ): IDefinitorInstance<SubType>;
886
-
887
- // Async modern overload: a Promise-returning callable handler stores an
888
- // async entry. It must precede the IDEF overload, whose callable void
889
- // result otherwise accepts async functions and erases their Promise.
985
+ constructHandler: DefineNewableOrCallable,
986
+ config?: DefineConfig
987
+ ): IDefinitorInstance<
988
+ F,
989
+ InstanceResult<F>,
990
+ T,
991
+ never,
992
+ Parent
993
+ >;
994
+
995
+ // Named overloads: define(TypeName, constructHandler, config?). An async
996
+ // handler stores an async entry.
890
997
  define<
891
998
  const Name extends string,
892
999
  N extends object,
@@ -896,18 +1003,57 @@ export interface RegistryHolderBase<
896
1003
  >(
897
1004
  this: RegistryHolderBase<T, Parent, Path>,
898
1005
  TypeName: Name,
899
- constructHandler: (this: N, ...args: Args) => PromiseLike<unknown>,
900
- config?: constructorOptions
1006
+ constructHandler: (this: N, ...args: Args) => never,
1007
+ config?: DefineConfig
1008
+ ): IDefinitorInstance<
1009
+ F,
1010
+ InstanceResult<F>,
1011
+ T & Record<ChildPath, RegistryEntry<F, ChildPath, Parent, Args>>,
1012
+ ChildPath,
1013
+ Parent,
1014
+ Args
1015
+ >;
1016
+ define<
1017
+ const Name extends string,
1018
+ N extends object,
1019
+ Args extends unknown[] = unknown[],
1020
+ F extends Proto<Parent, N> = Proto<Parent, N>,
1021
+ ChildPath extends string = ChildPathOf<Path, Name>
1022
+ >(
1023
+ this: RegistryHolderBase<T, Parent, Path>,
1024
+ TypeName: Name,
1025
+ constructHandler: AsyncIDEF<N, Args>,
1026
+ config?: DefineConfig
901
1027
  ): IDefinitorInstance<
902
1028
  F,
903
1029
  InstanceResult<F>,
904
1030
  T & Record<ChildPath, AsyncRegistryEntry<F, ChildPath, Parent, Args>>,
905
1031
  ChildPath,
906
1032
  Parent,
1033
+ Args,
1034
+ Promise<InstanceResult<F>>
1035
+ >;
1036
+ define<
1037
+ const Name extends string,
1038
+ N extends object,
1039
+ Args extends unknown[] = unknown[],
1040
+ F extends Proto<Parent, N> = Proto<Parent, N>,
1041
+ ChildPath extends string = ChildPathOf<Path, Name>
1042
+ >(
1043
+ this: RegistryHolderBase<T, Parent, Path>,
1044
+ TypeName: Name,
1045
+ constructHandler: IDEF<N, Args> | undefined,
1046
+ config?: DefineConfig
1047
+ ): IDefinitorInstance<
1048
+ F,
1049
+ InstanceResult<F>,
1050
+ T & Record<ChildPath, RegistryEntry<F, ChildPath, Parent, Args>>,
1051
+ ChildPath,
1052
+ Parent,
907
1053
  Args
908
1054
  >;
909
1055
 
910
- // Modern overload: define(TypeName, constructHandler, config?)
1056
+ // Named overload without a handler: define(TypeName, config?)
911
1057
  define<
912
1058
  const Name extends string,
913
1059
  N extends object,
@@ -917,8 +1063,7 @@ export interface RegistryHolderBase<
917
1063
  >(
918
1064
  this: RegistryHolderBase<T, Parent, Path>,
919
1065
  TypeName: Name,
920
- constructHandler?: IDEF<N, Args>,
921
- config?: constructorOptions
1066
+ config?: NoHandlerConfig
922
1067
  ): IDefinitorInstance<
923
1068
  F,
924
1069
  InstanceResult<F>,
@@ -928,8 +1073,9 @@ export interface RegistryHolderBase<
928
1073
  Args
929
1074
  >;
930
1075
 
931
- // Async lazy getter: the callback itself stays synchronous, but the
932
- // constructor it returns may produce a Promise on every construction.
1076
+ // Lazy getters, in the same order. The callback itself stays synchronous,
1077
+ // but the constructor it returns may produce a Promise on every
1078
+ // construction (the async form).
933
1079
  lazy<
934
1080
  const Name extends string,
935
1081
  N extends object,
@@ -939,16 +1085,36 @@ export interface RegistryHolderBase<
939
1085
  >(
940
1086
  this: RegistryHolderBase<T, Parent, Path>,
941
1087
  TypeName: Name,
942
- getter: () => (this: N, ...args: Args) => PromiseLike<unknown>,
943
- config?: constructorOptions
1088
+ getter: () => (this: N, ...args: Args) => never,
1089
+ config?: DefineConfig
944
1090
  ): IDefinitorInstance<
945
1091
  F,
946
1092
  InstanceResult<F>,
947
- T & Record<ChildPath, AsyncRegistryEntry<F, ChildPath, Parent, Args>>,
1093
+ T & Record<ChildPath, RegistryEntry<F, ChildPath, Parent, Args>>,
948
1094
  ChildPath,
949
1095
  Parent,
950
1096
  Args
951
1097
  >;
1098
+ lazy<
1099
+ const Name extends string,
1100
+ N extends object,
1101
+ Args extends unknown[],
1102
+ F extends Proto<Parent, N> = Proto<Parent, N>,
1103
+ ChildPath extends string = ChildPathOf<Path, Name>
1104
+ >(
1105
+ this: RegistryHolderBase<T, Parent, Path>,
1106
+ TypeName: Name,
1107
+ getter: () => AsyncIDEF<N, Args>,
1108
+ config?: DefineConfig
1109
+ ): IDefinitorInstance<
1110
+ F,
1111
+ InstanceResult<F>,
1112
+ T & Record<ChildPath, AsyncRegistryEntry<F, ChildPath, Parent, Args>>,
1113
+ ChildPath,
1114
+ Parent,
1115
+ Args,
1116
+ Promise<InstanceResult<F>>
1117
+ >;
952
1118
 
953
1119
  // Lazy getter: .lazy(TypeName, () => Constructor, config?)
954
1120
  // (the unnamed form is dropped: the callback is never called at define
@@ -963,7 +1129,7 @@ export interface RegistryHolderBase<
963
1129
  this: RegistryHolderBase<T, Parent, Path>,
964
1130
  TypeName: Name,
965
1131
  getter: LazyDef<N, Args>,
966
- config?: constructorOptions
1132
+ config?: DefineConfig
967
1133
  ): IDefinitorInstance<
968
1134
  F,
969
1135
  InstanceResult<F>,
@@ -993,6 +1159,7 @@ export interface RegistryHolder<
993
1159
  // Parent = instance type of the parent type (object for root types);
994
1160
  // threaded into registerHook's existentInstance typing
995
1161
  // Args = construction parameters declared by the handler
1162
+ // New = what `new` gives: the instance, or its Promise for an async handler
996
1163
  export interface IDefinitorInstance<
997
1164
  N extends object,
998
1165
  R extends InstanceResult<N> = InstanceResult<N>,
@@ -1000,8 +1167,9 @@ export interface IDefinitorInstance<
1000
1167
  Path extends string = '',
1001
1168
  Parent extends object = object,
1002
1169
  Args extends unknown[] = unknown[],
1170
+ New extends object = R,
1003
1171
  >
1004
- extends RegistryHolderBase<Registry, N, Path> {
1172
+ extends RegistryHolderBase<Registry, N, Path>, HookMethods<Parent, N> {
1005
1173
 
1006
1174
  TypeName: string;
1007
1175
  prototype: N;
@@ -1010,7 +1178,7 @@ export interface IDefinitorInstance<
1010
1178
  // so makes interface constructible
1011
1179
  // new(...args: unknown[]): { [key in keyof R]: R[key] };
1012
1180
  /** @throws {CreationError<R>} when construction fails */
1013
- new(...args: Args): R;
1181
+ new(...args: Args): New;
1014
1182
 
1015
1183
  // TODO: need check if line below works
1016
1184
  // also TS hinting check as well
@@ -1019,9 +1187,7 @@ export interface IDefinitorInstance<
1019
1187
 
1020
1188
  lookup: TypeLookup<Registry>;
1021
1189
 
1022
- decorate: (config?: constructorOptions) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
1023
-
1024
- registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, Parent, N>): void;
1190
+ decorate: (config?: DefineConfig) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
1025
1191
 
1026
1192
  subtypes: SubtypesMap;
1027
1193
 
@@ -1035,18 +1201,62 @@ export interface IDefinitorInstance<
1035
1201
  */
1036
1202
  }
1037
1203
 
1038
- // Type absorber function type - used for defining subtypes
1039
- // Supports multiple calling conventions:
1040
- // 1. Modern: define(TypeName, constructHandler, config?)
1041
- // 2. Legacy: define(constructHandler, config?) - TypeName from constructor name
1042
- // 3. Nested: parentType.define(TypeName, constructHandler, config?)
1204
+ // Type absorber function type - define() without a local registry.
1205
+ // The same three forms as RegistryHolderBase.define; each form ends with the
1206
+ // overload for an instance type given explicitly.
1043
1207
  export interface TypeAbsorber extends CallableFunction {
1208
+ // define(TypeName, constructHandler, config?)
1044
1209
  <T extends object, Args extends unknown[] = unknown[]>(
1045
1210
  this: unknown,
1046
- TypeOrTypeName: string | DefineNewableOrCallable,
1047
- constructHandlerOrConfig?: IDEF<T, Args> | object | boolean | DefineNewableOrCallable,
1048
- configOrUndefined?: constructorOptions | DefineNewableOrCallable | boolean
1211
+ TypeName: string,
1212
+ constructHandler: (this: T, ...args: Args) => never,
1213
+ config?: DefineConfig
1049
1214
  ): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
1215
+ <T extends object, Args extends unknown[] = unknown[]>(
1216
+ this: unknown,
1217
+ TypeName: string,
1218
+ constructHandler: AsyncIDEF<T, Args>,
1219
+ config?: DefineConfig
1220
+ ): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args, Promise<InstanceResult<T>>>;
1221
+ <T extends object, Args extends unknown[] = unknown[]>(
1222
+ this: unknown,
1223
+ TypeName: string,
1224
+ constructHandler: IDEF<T, Args> | undefined,
1225
+ config?: DefineConfig
1226
+ ): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
1227
+ <T extends object>(
1228
+ this: unknown,
1229
+ TypeName: string,
1230
+ constructHandler: DefineNewableOrCallable,
1231
+ config?: DefineConfig
1232
+ ): IDefinitorInstance<T>;
1233
+ // define(TypeName, config?)
1234
+ <T extends object, Args extends unknown[] = unknown[]>(
1235
+ this: unknown,
1236
+ TypeName: string,
1237
+ config?: NoHandlerConfig
1238
+ ): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
1239
+ // define(constructHandler, config?)
1240
+ <T extends object, Args extends unknown[] = unknown[]>(
1241
+ this: unknown,
1242
+ constructHandler: (this: T, ...args: Args) => never,
1243
+ config?: DefineConfig
1244
+ ): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
1245
+ <T extends object, Args extends unknown[] = unknown[]>(
1246
+ this: unknown,
1247
+ constructHandler: AsyncIDEF<T, Args>,
1248
+ config?: DefineConfig
1249
+ ): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args, Promise<InstanceResult<T>>>;
1250
+ <T extends object, Args extends unknown[] = unknown[]>(
1251
+ this: unknown,
1252
+ constructHandler: IDEF<T, Args>,
1253
+ config?: DefineConfig
1254
+ ): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
1255
+ <T extends object>(
1256
+ this: unknown,
1257
+ constructHandler: DefineNewableOrCallable,
1258
+ config?: DefineConfig
1259
+ ): IDefinitorInstance<T>;
1050
1260
  }
1051
1261
 
1052
1262
  // Lazy absorber function type - used for explicit lazy getter definitions
@@ -1057,14 +1267,7 @@ export interface LazyAbsorber extends CallableFunction {
1057
1267
  this: unknown,
1058
1268
  TypeName: string,
1059
1269
  getter: LazyDef<T, Args>,
1060
- config?: constructorOptions
1061
- ): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
1062
- <T extends object, Args extends unknown[] = unknown[]>(
1063
- this: unknown,
1064
- source: RegistryHolderBase<object, object, string>,
1065
- TypeName: string,
1066
- getter: LazyDef<T, Args>,
1067
- config?: constructorOptions
1270
+ config?: DefineConfig
1068
1271
  ): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
1069
1272
  }
1070
1273
 
@@ -1081,7 +1284,7 @@ export interface TypesCollection<
1081
1284
  // Decorator factory: .decorate() or .decorate(config)
1082
1285
  // Returns a class decorator that registers the class in this collection's registry.
1083
1286
  decorate(
1084
- config?: constructorOptions
1287
+ config?: DefineConfig
1085
1288
  ): <U extends Constructor<object>>(cstr: U) => DecoratedClass<
1086
1289
  U,
1087
1290
  T & Record<ConstructorName<U>, RegistryEntry<InstanceType<U>, ConstructorName<U>>>
@@ -1259,11 +1462,22 @@ export type Merge<E extends object, T extends object> = {
1259
1462
  K extends keyof T ? T[K] : E[K & keyof E];
1260
1463
  };
1261
1464
 
1465
+ // Keeps an `any` constructor off the async overload of apply/call/bind
1466
+ // (`C & NotAny<C>`), which it would match first.
1467
+ export type NotAny<C> = 0 extends (1 & C) ? never : unknown;
1468
+
1262
1469
  // Apply/Call/Bind function types
1263
1470
  // The return type merges the parent entity fields with the child constructor
1264
1471
  // fields (filtering out MnemonicaInstance method names), producing the same
1265
1472
  // `{ fields }` hover style as `new Type()`.
1266
1473
  export interface ApplyFunction extends CallableFunction {
1474
+ <E extends object, C extends Constructor<PromiseLike<object>>>(
1475
+ entity: E,
1476
+ Constructor: C & NotAny<C>,
1477
+ ... [ args ]: [] extends ConstructorParameters<C>
1478
+ ? [args?: ConstructorParameters<C>]
1479
+ : [args: ConstructorParameters<C>]
1480
+ ): Promise<InstanceResult<Merge<E, Awaited<InstanceType<C>>>>>;
1267
1481
  <E extends object, T extends object, Args extends unknown[]>(
1268
1482
  entity: E,
1269
1483
  Constructor: CtorParameter<T, Args>,
@@ -1272,6 +1486,11 @@ export interface ApplyFunction extends CallableFunction {
1272
1486
  }
1273
1487
 
1274
1488
  export interface CallFunction extends CallableFunction {
1489
+ <E extends object, C extends Constructor<PromiseLike<object>>>(
1490
+ entity: E,
1491
+ Constructor: C & NotAny<C>,
1492
+ ...args: ConstructorParameters<C>
1493
+ ): Promise<InstanceResult<Merge<E, Awaited<InstanceType<C>>>>>;
1275
1494
  <E extends object, T extends object, Args extends unknown[]>(
1276
1495
  entity: E,
1277
1496
  Constructor: CtorParameter<T, Args>,
@@ -1280,6 +1499,10 @@ export interface CallFunction extends CallableFunction {
1280
1499
  }
1281
1500
 
1282
1501
  export interface BindFunction extends CallableFunction {
1502
+ <E extends object, C extends Constructor<PromiseLike<object>>>(
1503
+ entity: E,
1504
+ Constructor: C & NotAny<C>
1505
+ ): (...args: ConstructorParameters<C>) => Promise<InstanceResult<Merge<E, Awaited<InstanceType<C>>>>>;
1283
1506
  <E extends object, T extends object, Args extends unknown[]>(
1284
1507
  entity: E,
1285
1508
  Constructor: CtorParameter<T, Args>
@@ -1379,14 +1602,19 @@ export interface MnemonicaModule<Registry extends object = {}>
1379
1602
  apply: ApplyFunction;
1380
1603
  call: CallFunction;
1381
1604
  bind: BindFunction;
1382
- decorate: <T extends Constructor<object> | constructorOptions | undefined = undefined>(
1605
+ decorate: <T extends Constructor<object> | NoHandlerConfig | undefined = undefined>(
1383
1606
  target?: T,
1384
- config?: constructorOptions
1607
+ config?: DefineConfig
1385
1608
  ) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
1386
1609
  registerHook: <
1387
1610
  T extends object,
1388
- HT extends hooksTypes = hooksTypes
1389
- >(Constructor: IDEF<T>, hookType: HT, cb: typedHook<HT, object, T>) => void;
1611
+ HT extends hookName = hooksTypes
1612
+ >(Constructor: HookableConstructor<T>, hookType: HT, cb: typedHook<HT, object, T>) => void;
1613
+ unregisterHook: (
1614
+ Constructor: Pick<HookMethods, 'unregisterHook'>,
1615
+ hookType: hookName,
1616
+ cb: AnyHookCallback
1617
+ ) => boolean;
1390
1618
 
1391
1619
  // Descriptors
1392
1620
  defaultTypes: TypesCollection<Registry>;
@@ -1408,6 +1636,7 @@ export interface MnemonicaModule<Registry extends object = {}>
1408
1636
  WRONG_STACK_CLEANER: MnemonicaErrorConstructor;
1409
1637
 
1410
1638
  // Constants
1639
+ defaultOptions: Required<constructorOptions>;
1411
1640
  MNEMONICA: string;
1412
1641
  MNEMOSYNE: string;
1413
1642
  SymbolParentType: symbol;