mnemonica 1.3.7 → 1.3.9

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.
Files changed (57) hide show
  1. package/FOR_HUMANS.md +79 -7
  2. package/build/api/errors/exceptionConstructor.js +3 -3
  3. package/build/api/errors/index.d.ts +3 -7
  4. package/build/api/errors/index.js +3 -5
  5. package/build/api/errors/throwModificationError.js +2 -4
  6. package/build/api/hooks/HookInvocation.js +1 -1
  7. package/build/api/hooks/flowCheckers.d.ts +3 -3
  8. package/build/api/hooks/flowCheckers.js +1 -1
  9. package/build/api/hooks/invokeHook.js +1 -1
  10. package/build/api/index.d.ts +3 -3
  11. package/build/api/types/InstanceCreator.js +6 -7
  12. package/build/api/types/InstanceModificator.js +6 -3
  13. package/build/api/types/TypeProxy.js +2 -2
  14. package/build/api/types/compileNewModificatorFunctionBody.js +1 -1
  15. package/build/api/types/index.d.ts +3 -3
  16. package/build/api/types/index.js +32 -19
  17. package/build/api/utils/index.d.ts +2 -2
  18. package/build/api/utils/index.js +31 -18
  19. package/build/constants/index.js +4 -1
  20. package/build/descriptors/errors/index.js +1 -1
  21. package/build/descriptors/types/index.js +21 -8
  22. package/build/index.d.ts +11 -13
  23. package/build/index.js +17 -18
  24. package/build/types/index.d.ts +116 -23
  25. package/build/types/index.js +1 -1
  26. package/build/utils/collectConstructors.js +1 -1
  27. package/build/utils/fork.d.ts +2 -1
  28. package/build/utils/fork.js +1 -1
  29. package/build/utils/index.js +3 -1
  30. package/build/utils/lineage.d.ts +2 -0
  31. package/build/utils/lineage.js +156 -0
  32. package/build/utils/parse.js +1 -1
  33. package/docs/UTILS.md +41 -2
  34. package/docs/async-constructors.md +6 -0
  35. package/package.json +4 -2
  36. package/src/api/errors/exceptionConstructor.ts +7 -5
  37. package/src/api/errors/index.ts +17 -16
  38. package/src/api/errors/throwModificationError.ts +4 -5
  39. package/src/api/hooks/HookInvocation.ts +2 -1
  40. package/src/api/hooks/flowCheckers.ts +8 -3
  41. package/src/api/hooks/invokeHook.ts +1 -1
  42. package/src/api/types/InstanceCreator.ts +20 -10
  43. package/src/api/types/InstanceModificator.ts +19 -4
  44. package/src/api/types/TypeProxy.ts +11 -2
  45. package/src/api/types/compileNewModificatorFunctionBody.ts +6 -0
  46. package/src/api/types/index.ts +84 -28
  47. package/src/api/utils/index.ts +60 -24
  48. package/src/constants/index.ts +6 -0
  49. package/src/descriptors/errors/index.ts +4 -2
  50. package/src/descriptors/types/index.ts +44 -17
  51. package/src/index.ts +86 -52
  52. package/src/types/index.ts +262 -36
  53. package/src/utils/collectConstructors.ts +0 -9
  54. package/src/utils/fork.ts +2 -1
  55. package/src/utils/index.ts +6 -1
  56. package/src/utils/lineage.ts +220 -0
  57. package/src/utils/parse.ts +2 -1
@@ -25,6 +25,69 @@ export type CtorParameter<T> = IDEF<T> | { new (...args: never[]): T };
25
25
  // Used by the explicit .lazy() API.
26
26
  export type LazyDef<T, Args extends unknown[] = unknown[]> = () => IDEF<T, Args>;
27
27
 
28
+ // define() handler in callable (plain function) form: the runtime invokes
29
+ // it with the fresh instance as `this` and the construction arguments as
30
+ // args. Parameters are never[] so every handler parameter list stays
31
+ // assignable by contravariance (the AnyConstructor trick); no `this`
32
+ // annotation so handlers declaring their own `this: Instance` assign too.
33
+ export interface DefineCallable extends CallableFunction {
34
+ (...args: never[]): unknown;
35
+ }
36
+
37
+ // define() handler in newable (class) form: the runtime constructs it with
38
+ // the construction arguments; the constructed object becomes the instance.
39
+ // never[] parameters accept every constructor parameter list, same
40
+ // contravariance reason as DefineCallable.
41
+ export interface DefineNewable extends NewableFunction {
42
+ new (...args: never[]): object;
43
+ }
44
+
45
+ // A define() handler in either form — the pair named once for the
46
+ // define()/lookup()/decorate overload unions.
47
+ export type DefineNewableOrCallable = DefineNewable | DefineCallable;
48
+
49
+ // The constructor reference every instance/prototype carries as
50
+ // `.constructor`. Signature-free on purpose: the Object interface types
51
+ // `.constructor` as bare Function (no construct signature of its own), and
52
+ // that reference must stay assignable here.
53
+ export interface InstanceConstructor extends NewableFunction {}
54
+
55
+ // A hook callback as seen by a REGISTERING surface that only forwards it
56
+ // (HookableConstructor's registerHook, the WithHookRegister member check):
57
+ // it consumes whatever opts the caller's typed hook carries and returns
58
+ // unknown. never[] parameters keep every hook shape assignable by
59
+ // contravariance, including generic typedHook<HT> instantiations (a
60
+ // parameter typed as the plain `hook` interface would reject those).
61
+ export interface AnyHookCallback extends CallableFunction {
62
+ (...args: never[]): unknown;
63
+ }
64
+
65
+ // A utility method as exposed on the `utils` collection — the
66
+ // index-signature fallback for utilities beyond the declared set. Plain
67
+ // callables: any argument list, unknown result.
68
+ export interface UtilFunction extends CallableFunction {
69
+ (...args: never[]): unknown;
70
+ }
71
+
72
+ // The minimal source shape the free lookup() resolves against: any value
73
+ // carrying a lookup method with the untyped fallback signature
74
+ // (a TypesCollection, a type constructor, or the mnemonica module object).
75
+ export interface LookupSource {
76
+ lookup(path: string): TypeClass | undefined;
77
+ }
78
+
79
+ // The modification-error sink handed through the creator object: consumes
80
+ // the construction error, produces nothing (the construction aborts).
81
+ export interface ThrowModificationError extends CallableFunction {
82
+ (error: Error): void;
83
+ }
84
+
85
+ // The bound fork invoker that instance.fork() returns: consumes fork
86
+ // arguments, produces a fresh instance of the forked type.
87
+ export interface ForkInvoker<T extends object> extends CallableFunction {
88
+ (this: object, ...forkArgs: unknown[]): T;
89
+ }
90
+
28
91
  // Error message types - all error messages are strings
29
92
  export type ErrorMessageKey =
30
93
  | 'BASE_ERROR_MESSAGE'
@@ -50,13 +113,21 @@ export interface hook extends CallableFunction {
50
113
  (opts: hooksOpts): unknown;
51
114
  }
52
115
 
53
- // Error constructor from constructError - constructable function with prototype
54
- export interface MnemonicaErrorConstructor {
55
- new(addition?: string, stack?: string | string[]): Error;
56
- (name: string): Error;
57
- prototype: {
58
- constructor: MnemonicaErrorConstructor;
59
- };
116
+ // Flow checker callback registered via registerFlowChecker: invoked with
117
+ // the hook invocation record after each hook run; its return value is
118
+ // collected into the invocation results but otherwise unused.
119
+ export interface FlowChecker extends CallableFunction {
120
+ (opts: object): unknown;
121
+ }
122
+
123
+ // An error class made by constructError(name, message) — the lib.es5
124
+ // ErrorConstructor idiom, `new`-only (a class cannot be called without it).
125
+ // Each class carries one fixed message (constants ErrorMessages); `addition`
126
+ // is the per-throw detail, appended as `${message} : ${addition}`; `stack`
127
+ // lines, when given, are prepended to the captured stack.
128
+ export interface MnemonicaErrorConstructor extends NewableFunction {
129
+ new (addition?: string, stack?: string[]): MnemonicaError;
130
+ readonly prototype: MnemonicaError;
60
131
  }
61
132
 
62
133
  // Errors types map - indexable record of error constructors
@@ -79,6 +150,29 @@ export interface ErrorProps {
79
150
  error?: unknown;
80
151
  }
81
152
 
153
+ /**
154
+ * What a failed construction of `T` throws. TypeScript has no checked
155
+ * exceptions, so `new T()` is typed by its happy path only; this names the
156
+ * other path for the `catch` side.
157
+ *
158
+ * - `T & Error` — the errored instance (`blockErrors: true`, the default):
159
+ * the failed construction still produces a `T` layer, and the original
160
+ * error is linked in as the end of its prototype chain, so the thrown
161
+ * value is `instanceof T` AND `instanceof Error`. Its data is read via
162
+ * `getProps(error)` (`ErrorProps`).
163
+ * - `Error` — the original error, propagated unwrapped
164
+ * (`blockErrors: false`), or a mnemonica error thrown before construction.
165
+ *
166
+ * Async constructors reject with the same values.
167
+ *
168
+ * @example
169
+ * function report (failed: CreationError<Widget>) {
170
+ * if (failed instanceof Widget) { failed.size; failed.message; }
171
+ * }
172
+ * try { new Widget(1); } catch (e) { if (e instanceof Error) report(e); }
173
+ */
174
+ export type CreationError<T extends object> = ( T & Error ) | Error;
175
+
82
176
  /**
83
177
  * Internal Type Constructor.
84
178
  * "Type" here is used in the Computer Science sense — an interface/contract
@@ -87,6 +181,7 @@ export interface ErrorProps {
87
181
  * shape, distinct from user-facing TypeConstructor below.
88
182
  */
89
183
  export interface _Internal_TC_<ConstructorInstance extends object> {
184
+ /** @throws {CreationError<ConstructorInstance>} when construction fails */
90
185
  new(...args: unknown[]): ConstructorInstance;
91
186
  (this: ConstructorInstance, ...args: unknown[]): ConstructorInstance;
92
187
  readonly prototype: ConstructorInstance & {
@@ -202,6 +297,50 @@ export type hooksOpts<P = object, T = P> = {
202
297
  creator?: { throwModificationError(error: Error): void };
203
298
  };
204
299
 
300
+ // Hook options typed per hookType — the compile-time face of the runtime
301
+ // hookData built in InstanceCreator (invokePreHooks / invokePostHooks).
302
+ // P = existent (parent) instance, T = inherited (created) instance.
303
+ // preCreation fires BEFORE the created instance exists, so its opts carry
304
+ // no inheritedInstance and no creator — accessing them is a compile error.
305
+ export type typedHookOpts<HT extends hooksTypes, P extends object, T extends object> =
306
+ HT extends 'preCreation'
307
+ ? {
308
+ TypeName: string;
309
+ type: TypeDef;
310
+ args: unknown[];
311
+ existentInstance: P;
312
+ }
313
+ : {
314
+ TypeName: string;
315
+ type: TypeDef;
316
+ args: unknown[];
317
+ existentInstance: P;
318
+ inheritedInstance: T;
319
+ creator: { throwModificationError(error: Error): void };
320
+ };
321
+
322
+ // Typed hook callback — the cb of a registerHook call bound to a known type:
323
+ // the callback's opts are inferred from the hookType and the parent/instance
324
+ // pair the hook is registered on. Collection-wide (global) handlers stay on
325
+ // the untyped `hook` above by design.
326
+ export interface typedHook<
327
+ HT extends hooksTypes,
328
+ P extends object = object,
329
+ T extends object = object
330
+ > extends CallableFunction {
331
+ (opts: typedHookOpts<HT, P, T>): unknown;
332
+ }
333
+
334
+ // Constructor surface the free registerHook() accepts: any constructor of T
335
+ // instances with a registerHook method. Deliberately NOT DecoratedClass<T> —
336
+ // inference through DecoratedClass's InstanceType<T> conditional picks a wrong
337
+ // T candidate, and inferring from a generic registerHook member resets T to
338
+ // its default; here T is inferred from Constructor<T>'s return position and
339
+ // the precise cb typing comes from the free function's own cb parameter.
340
+ export type HookableConstructor<T extends object> = Constructor<T> & {
341
+ registerHook(hookType: hooksTypes, cb: AnyHookCallback): void;
342
+ };
343
+
205
344
  // Callback passed into ModificationConstructor to attach internal props to the prototype
206
345
  export interface AddPropsCallback extends CallableFunction {
207
346
  (proto: object): void;
@@ -407,6 +546,24 @@ export type LookedUpConstructor<
407
546
  lookup: NestedTypeLookup<Registry, Path>;
408
547
  };
409
548
 
549
+ // Typed registerHook surface for lookup results whose registry entries are
550
+ // bare constructors (tactica-emitted or hand-written TypeRegistry entries):
551
+ // their lookup results would otherwise lack registerHook entirely. The
552
+ // created-instance type is recovered from the entry's construct/call return.
553
+ // Constructors that already carry a typed registerHook (builder-created
554
+ // RegistryEntry values) pass through unchanged.
555
+ export type WithHookRegister<C> = C extends { registerHook: AnyHookCallback }
556
+ ? C
557
+ : C extends { new (...args: never[]): infer R }
558
+ ? R extends object
559
+ ? C & { registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, object, R>): void }
560
+ : C
561
+ : C extends { (...args: never[]): infer R }
562
+ ? R extends object
563
+ ? C & { registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, object, R>): void }
564
+ : C
565
+ : C;
566
+
410
567
  // Result of a typed lookup: the augmented constructor plus a `lookup` method
411
568
  // scoped to the constructor's own type path for relative subtype lookups.
412
569
  export type LookupResult<
@@ -587,6 +744,56 @@ export interface EmptyParsed {
587
744
  parent: undefined;
588
745
  }
589
746
 
747
+ // --- lineage (the lethe cross-language export) -----------------------------
748
+
749
+ // A JSON-safe value in the lethe lineage graph: instances export as $ref,
750
+ // non-JSON values as tagged placeholders — never an error.
751
+ export type LineageValue =
752
+ | null
753
+ | boolean
754
+ | string
755
+ | number
756
+ | LineageValue[]
757
+ | { $ref: string }
758
+ | { '$mnemonica': 'unsupported'; kind: string }
759
+ | { [ key: string ]: LineageValue };
760
+
761
+ // Where a level's type was DECLARED — not just its name (names collide
762
+ // across levels and collections).
763
+ export interface LineageTypeRef {
764
+ collection: string;
765
+ path: string;
766
+ }
767
+
768
+ export interface LineageNode {
769
+ type: LineageTypeRef;
770
+ // the fields this level set itself — shadowed values stay with their
771
+ // writer
772
+ own: { [ key: string ]: LineageValue };
773
+ // the parent's id, null at the root
774
+ parent: string | null;
775
+ // construction args — present only when lineage() gets { args: true }
776
+ args?: LineageValue;
777
+ // opt-in metadata (e.g. __timestamp__) — present only when
778
+ // lineage() gets { props: [...] }
779
+ props?: { [ key: string ]: LineageValue };
780
+ }
781
+
782
+ // The lethe lineage graph: version "1", heads in argument order, every
783
+ // reachable instance deduplicated at any depth.
784
+ export interface LineageGraph {
785
+ version: '1';
786
+ heads: string[];
787
+ nodes: { [ id: string ]: LineageNode };
788
+ }
789
+
790
+ export interface LineageOptions {
791
+ /** include construction args as node.args */
792
+ args?: boolean;
793
+ /** opt-in getProps keys exported as node.props (e.g. [ '__timestamp__' ]) */
794
+ props?: string[];
795
+ }
796
+
590
797
  // Sibling type accessor
591
798
  export interface SiblingAccessor {
592
799
  (SiblingTypeName: string): TypeClass | undefined;
@@ -648,10 +855,19 @@ export type InstanceResult<
648
855
  // It preserves the dotted `Path` so that `.define()` on a looked-up constructor
649
856
  // computes the correct child path, but it omits the full `GlobalRegistry`
650
857
  // generic to keep hover tooltips readable.
858
+ // `Parent` is the instance type of the type's parent (object for root types),
859
+ // threaded so looked-up constructors type their registerHook callbacks'
860
+ // existentInstance.
651
861
  export interface RegistryEntry<
652
862
  F extends object,
653
- Path extends string = ''
654
- > extends _Internal_TC_<F>, RegistryHolderBase<{}, F, Path> {}
863
+ Path extends string = '',
864
+ Parent extends object = object
865
+ > extends _Internal_TC_<F>, RegistryHolderBase<{}, F, Path> {
866
+ // typed hook registration — preserved through ReplaceConstructorInstance's
867
+ // member preservation, so lookup() results of builder-typed registries
868
+ // get registerHook with inferred opts
869
+ registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, Parent, F>): void;
870
+ }
655
871
 
656
872
  // Registry holder base - provides the accumulating .define() method.
657
873
  // `Parent` tracks the instance type of the last defined constructor, so chained
@@ -665,9 +881,9 @@ export interface RegistryHolderBase<
665
881
  // Legacy overload: define(constructHandler, config?)
666
882
  define<SubType extends object>(
667
883
  this: RegistryHolderBase<T, Parent, Path>,
668
- TypeOrTypeName: CallableFunction,
669
- constructHandlerOrConfig?: IDEF<SubType> | object | boolean | CallableFunction,
670
- configOrUndefined?: constructorOptions | CallableFunction | boolean
884
+ TypeOrTypeName: DefineNewableOrCallable,
885
+ constructHandlerOrConfig?: IDEF<SubType> | object | boolean | DefineNewableOrCallable,
886
+ configOrUndefined?: constructorOptions | DefineNewableOrCallable | boolean
671
887
  ): IDefinitorInstance<SubType>;
672
888
 
673
889
  // Modern overload: define(TypeName, constructHandler, config?)
@@ -685,8 +901,9 @@ export interface RegistryHolderBase<
685
901
  ): IDefinitorInstance<
686
902
  F,
687
903
  InstanceResult<F>,
688
- T & Record<ChildPath, RegistryEntry<F, ChildPath>>,
689
- ChildPath
904
+ T & Record<ChildPath, RegistryEntry<F, ChildPath, Parent>>,
905
+ ChildPath,
906
+ Parent
690
907
  >;
691
908
 
692
909
  // Explicit lazy getter: .lazy(() => Constructor, config?)
@@ -710,8 +927,9 @@ export interface RegistryHolderBase<
710
927
  ): IDefinitorInstance<
711
928
  F,
712
929
  InstanceResult<F>,
713
- T & Record<ChildPath, RegistryEntry<F, ChildPath>>,
714
- ChildPath
930
+ T & Record<ChildPath, RegistryEntry<F, ChildPath, Parent>>,
931
+ ChildPath,
932
+ Parent
715
933
  >;
716
934
  }
717
935
 
@@ -731,11 +949,14 @@ export interface RegistryHolder<
731
949
  // R = wrapped result instance type
732
950
  // Registry = typed registry map for lookup()
733
951
  // Path = dotted type path of this constructor (empty for root types)
952
+ // Parent = instance type of the parent type (object for root types);
953
+ // threaded into registerHook's existentInstance typing
734
954
  export interface IDefinitorInstance<
735
955
  N extends object,
736
956
  R extends InstanceResult<N> = InstanceResult<N>,
737
957
  Registry extends object = GlobalRegistry,
738
- Path extends string = ''
958
+ Path extends string = '',
959
+ Parent extends object = object,
739
960
  >
740
961
  extends RegistryHolderBase<Registry, N, Path> {
741
962
 
@@ -746,6 +967,7 @@ export interface IDefinitorInstance<
746
967
  // the line below declares a `new ...` invocation,
747
968
  // so makes interface constructible
748
969
  // new(...args: unknown[]): { [key in keyof R]: R[key] };
970
+ /** @throws {CreationError<R>} when construction fails */
749
971
  new(...args: unknown[]): R;
750
972
 
751
973
 
@@ -758,7 +980,7 @@ export interface IDefinitorInstance<
758
980
 
759
981
  decorate: (config?: constructorOptions) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
760
982
 
761
- registerHook(hookType: hooksTypes, cb: hook): void;
983
+ registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, Parent, N>): void;
762
984
 
763
985
  subtypes: SubtypesMap;
764
986
 
@@ -780,9 +1002,9 @@ export interface IDefinitorInstance<
780
1002
  export interface TypeAbsorber extends CallableFunction {
781
1003
  <T extends object>(
782
1004
  this: unknown,
783
- TypeOrTypeName: string | CallableFunction,
784
- constructHandlerOrConfig?: IDEF<T> | object | boolean | CallableFunction,
785
- configOrUndefined?: constructorOptions | CallableFunction | boolean
1005
+ TypeOrTypeName: string | DefineNewableOrCallable,
1006
+ constructHandlerOrConfig?: IDEF<T> | object | boolean | DefineNewableOrCallable,
1007
+ configOrUndefined?: constructorOptions | DefineNewableOrCallable | boolean
786
1008
  ): IDefinitorInstance<T>;
787
1009
  }
788
1010
 
@@ -801,9 +1023,9 @@ export interface LazyAbsorber extends CallableFunction {
801
1023
  ): IDefinitorInstance<T>;
802
1024
  <T extends object>(
803
1025
  this: unknown,
804
- arg1: string | LazyDef<T>,
805
- arg2: LazyDef<T> | constructorOptions,
806
- arg3?: constructorOptions
1026
+ TypeNameOrGetter: string | LazyDef<T>,
1027
+ getterOrConfig: LazyDef<T> | constructorOptions,
1028
+ namedFormConfig?: constructorOptions
807
1029
  ): IDefinitorInstance<T>;
808
1030
  <T extends object>(
809
1031
  this: unknown,
@@ -821,9 +1043,9 @@ export interface LazyAbsorber extends CallableFunction {
821
1043
  <T extends object>(
822
1044
  this: unknown,
823
1045
  source: RegistryHolderBase<object, object, string>,
824
- arg1: string | LazyDef<T>,
825
- arg2: LazyDef<T> | constructorOptions,
826
- arg3?: constructorOptions
1046
+ TypeNameOrGetter: string | LazyDef<T>,
1047
+ getterOrConfig: LazyDef<T> | constructorOptions,
1048
+ namedFormConfig?: constructorOptions
827
1049
  ): IDefinitorInstance<T>;
828
1050
  }
829
1051
 
@@ -854,7 +1076,7 @@ export interface Hookable {
854
1076
  hooks: Record<string, Set<hook>>;
855
1077
  invokeHook(hookType: hooksTypes, opts: hooksOpts): Set<unknown>;
856
1078
  registerHook(hookType: hooksTypes, cb: hook): void;
857
- registerFlowChecker(cb: (opts: object) => unknown): void;
1079
+ registerFlowChecker(cb: FlowChecker): void;
858
1080
  }
859
1081
 
860
1082
  // createTypesCollection function type
@@ -881,15 +1103,15 @@ export interface MnemonicaConstructor extends NewableFunction {
881
1103
  // Type descriptor instance — internal shape of TypeDescriptor objects
882
1104
  export interface TypeDescriptorDefine extends CallableFunction {
883
1105
  (
884
- TypeOrTypeName: string | CallableFunction,
885
- constructHandlerOrConfig?: CallableFunction | object,
1106
+ TypeOrTypeName: string | DefineNewableOrCallable,
1107
+ constructHandlerOrConfig?: DefineNewableOrCallable | object,
886
1108
  config?: object
887
1109
  ): TypeClass;
888
1110
  }
889
1111
 
890
1112
  export interface TypeDescriptorLazy extends CallableFunction {
891
1113
  (
892
- getter: CallableFunction,
1114
+ getter: LazyDef<object>,
893
1115
  config?: object
894
1116
  ): TypeClass;
895
1117
  }
@@ -979,9 +1201,9 @@ export interface UtilsCollection {
979
1201
  pick<T extends object, K extends keyof T>(instance: T, ...args: (K | K[])[]): { [P in K]: T[P] } & {};
980
1202
  pick<T extends object>(instance: T, ...args: (string | string[])[]): Record<string, unknown>;
981
1203
  clone<T extends object>(instance: T): T;
982
- fork<T extends object>(instance: T): (this: object, ...forkArgs: unknown[]) => T;
1204
+ fork<T extends object>(instance: T): ForkInvoker<T>;
983
1205
  sibling(instance: object): SiblingAccessor;
984
- collectConstructors: (instance: object, flat?: boolean) => (CallableFunction | string)[];
1206
+ collectConstructors: (instance: object, flat?: boolean) => (DefineNewableOrCallable | string)[];
985
1207
  merge<A extends object, B extends object>(
986
1208
  a: A,
987
1209
  b: B,
@@ -990,6 +1212,7 @@ export interface UtilsCollection {
990
1212
  parse<T extends object>(self: T): Parsed<T>;
991
1213
  parse(self: null): EmptyParsed;
992
1214
  deepParse<T extends object>(instance: T): Array<Parsed<T>>;
1215
+ lineage(instances: object[], options?: LineageOptions): LineageGraph;
993
1216
  parent<T extends object>(instance: T): object | null;
994
1217
  parent<T extends object, K extends ParentPathOfInstance<T> & string>(
995
1218
  instance: T,
@@ -997,7 +1220,7 @@ export interface UtilsCollection {
997
1220
  ): InstanceOfTypeRegistry<K> | undefined;
998
1221
  parent<T extends object>(instance: T, path: string): object | undefined;
999
1222
  toJSON<T extends object>(instance: T): string;
1000
- [key: string]: CallableFunction;
1223
+ [key: string]: UtilFunction;
1001
1224
  }
1002
1225
 
1003
1226
  // Main mnemonica module interface - represents the exported module object.
@@ -1013,7 +1236,10 @@ export interface MnemonicaModule<Registry extends object = {}>
1013
1236
  target?: T,
1014
1237
  config?: constructorOptions
1015
1238
  ) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
1016
- registerHook: <T extends object>(Constructor: IDEF<T>, hookType: hooksTypes, cb: hook) => void;
1239
+ registerHook: <
1240
+ T extends object,
1241
+ HT extends hooksTypes = hooksTypes
1242
+ >(Constructor: IDEF<T>, hookType: HT, cb: typedHook<HT, object, T>) => void;
1017
1243
 
1018
1244
  // Descriptors
1019
1245
  defaultTypes: TypesCollection<Registry>;
@@ -1049,7 +1275,7 @@ export interface MnemonicaModule<Registry extends object = {}>
1049
1275
  getProps: (instance: object) => PropsType | undefined;
1050
1276
  setProps: (instance: object, values: object) => string[] | false;
1051
1277
  findSubTypeFromParent: (instance: object | undefined, subType: string) => object | null;
1052
- isClass: (fn: CallableFunction) => boolean;
1278
+ isClass: (fn: DefineNewableOrCallable) => boolean;
1053
1279
 
1054
1280
  // createTypesCollection
1055
1281
  createTypesCollection: CreateTypesCollectionFunction;
@@ -70,15 +70,6 @@ export const collectConstructors = (self: object, asSequence = false) => {
70
70
  addToSequence('Object: null prototype');
71
71
  break;
72
72
  }
73
- /*
74
- TODO: show full chain and test it !!!
75
- else {
76
- // proto may be either empty object or null
77
- // so typeof will always be 'object'
78
- // eslint-disable-next-line no-lonely-if
79
- addToSequence('... plain object ...');
80
- }
81
- */
82
73
  // so here we go deeper to the chain
83
74
  }
84
75
  return constructors;
package/src/utils/fork.ts CHANGED
@@ -5,10 +5,11 @@ import {
5
5
  } from '../api/types/Props';
6
6
  import { InstanceCreator } from '../api/types/InstanceCreator';
7
7
  import TypesUtils from '../api/utils/index';
8
+ import type { ForkInvoker } from '../types';
8
9
 
9
10
  const { reflectPrimitiveWrappers } = TypesUtils;
10
11
 
11
- export const fork = <T extends object>(instance: T): (this: object, ...forkArgs: unknown[]) => T => {
12
+ export const fork = <T extends object>(instance: T): ForkInvoker<T> => {
12
13
 
13
14
  const props = getProps(instance) as Props;
14
15
 
@@ -16,6 +16,7 @@ import { clone } from './clone';
16
16
  import { toJSON } from './toJSON';
17
17
  import { parse } from './parse';
18
18
  import { deepParse } from './deepParse';
19
+ import { lineage } from './lineage';
19
20
  import { merge } from './merge';
20
21
 
21
22
  const utilsUnWrapped = {
@@ -33,6 +34,7 @@ const utilsUnWrapped = {
33
34
 
34
35
  parse,
35
36
  deepParse,
37
+ lineage,
36
38
  merge,
37
39
 
38
40
  collectConstructors,
@@ -44,6 +46,9 @@ const wrapThis = ( method: WrappableMethod ) => {
44
46
  const instanceContext = instance !== undefined ? instance : this;
45
47
  let wrapResult: unknown;
46
48
  if ( new.target ) {
49
+ // wrapThis forwards `new` to the util; WrappableMethod (a plain
50
+ // function type) declares no construct signature, so the cast
51
+ // bridges that construct call
47
52
  wrapResult = new (method as unknown as new (...a: unknown[]) => unknown)(
48
53
  instanceContext,
49
54
  ...args
@@ -63,7 +68,7 @@ export const utils = {
63
68
 
64
69
  ...Object.entries( utilsUnWrapped )
65
70
  .reduce(
66
- ( methods: { [ index: string ]: CallableFunction }, util ) => {
71
+ ( methods: { [ index: string ]: WrappableMethod }, util ) => {
67
72
  const [ name, fn ] = util;
68
73
  methods[ name ] = wrapThis( fn );
69
74
  return methods;