mnemonica 1.1.0 → 1.2.1

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 (78) hide show
  1. package/.ai/AGENTS.md +31 -18
  2. package/.ai/DEBUG.md +7 -8
  3. package/.ai/ONBOARDING.md +24 -7
  4. package/.ai/PROTOTYPE-CHAIN.md +33 -2
  5. package/.ai/{ask/AGENTS.md → mode-ask.md} +1 -1
  6. package/.ai/{orchestrator/AGENTS.md → mode-orchestrator.md} +2 -19
  7. package/{docs → .ai}/performance-vs-security.md +1 -1
  8. package/.ai/{rules-skill/async-constructors.md → rules-async-constructors.md} +146 -0
  9. package/.ai/{rules/CODING.md → rules-coding.md} +19 -2
  10. package/.ai/{rules/CONTEXT-CONDENSING.md → rules-context-condensing.md} +2 -2
  11. package/.ai/{rules-skill/contributing.md → rules-contributing.md} +35 -0
  12. package/.ai/{rules-skill/define-patterns.md → rules-define-patterns.md} +39 -0
  13. package/.ai/{rules-skill/instance-methods.md → rules-instance-methods.md} +1 -1
  14. package/.ai/rules-reminders.md +19 -0
  15. package/.ai/{rules-skill/type-system.md → rules-type-system.md} +1 -1
  16. package/{docs → .ai}/theory-of-operations.md +4 -4
  17. package/AGENTS.md +76 -32
  18. package/CONTRIBUTING.md +4 -18
  19. package/FOR_HUMANS.md +123 -83
  20. package/README.md +65 -28
  21. package/SKILL.md +27 -22
  22. package/build/api/errors/index.js +7 -5
  23. package/build/api/index.d.ts +1 -0
  24. package/build/api/index.js +2 -1
  25. package/build/api/types/InstanceCreator.js +16 -5
  26. package/build/api/types/Mnemosyne.d.ts +3 -14
  27. package/build/api/types/Mnemosyne.js +21 -8
  28. package/build/api/types/Props.js +1 -2
  29. package/build/api/types/TypeProxy.d.ts +1 -1
  30. package/build/api/types/TypeProxy.js +9 -9
  31. package/build/api/types/compileNewModificatorFunctionBody.js +3 -5
  32. package/build/api/types/index.d.ts +5 -0
  33. package/build/api/types/index.js +77 -33
  34. package/build/api/utils/index.d.ts +1 -0
  35. package/build/api/utils/index.js +13 -3
  36. package/build/descriptors/types/index.js +46 -2
  37. package/build/index.d.ts +14 -3
  38. package/build/index.js +34 -7
  39. package/build/types/index.d.ts +24 -4
  40. package/build/types/index.js +1 -1
  41. package/build/utils/parent.js +35 -5
  42. package/docs/ai-learning-trajectory.md +2 -3
  43. package/docs/async-constructors.md +1 -1
  44. package/docs/decorate.md +123 -0
  45. package/docs/empathy-in-ai.md +2 -3
  46. package/docs/hott-primer.md +1 -1
  47. package/docs/prototype-chain.md +4 -0
  48. package/docs/purpose.md +3 -3
  49. package/{.ai/TACTICA-DEEP-DIVE.md → docs/tactica-deep-dive.md} +44 -1
  50. package/docs/tactica-pattern.md +1 -2
  51. package/docs/typed-lookup.md +183 -50
  52. package/package.json +8 -8
  53. package/src/api/errors/index.ts +10 -4
  54. package/src/api/index.ts +2 -8
  55. package/src/api/types/InstanceCreator.ts +28 -6
  56. package/src/api/types/Mnemosyne.ts +34 -15
  57. package/src/api/types/Props.ts +4 -4
  58. package/src/api/types/TypeProxy.ts +10 -9
  59. package/src/api/types/compileNewModificatorFunctionBody.ts +12 -4
  60. package/src/api/types/index.ts +136 -69
  61. package/src/api/utils/index.ts +27 -9
  62. package/src/descriptors/types/index.ts +81 -3
  63. package/src/index.ts +150 -7
  64. package/src/types/index.ts +120 -10
  65. package/src/utils/parent.ts +48 -4
  66. package/.ai/CODE.md +0 -111
  67. package/.ai/TACTICA-RULES.md +0 -103
  68. package/.ai/async_init.md +0 -94
  69. package/.ai/rules/REMINDERS.md +0 -63
  70. package/.ai/rules-skill/lookup-typed.md +0 -82
  71. package/.ai/rules-skill/proxy-architecture.md +0 -63
  72. package/.ai/task-templates/new-feature.md +0 -46
  73. /package/.ai/{rules-skill/code-style.md → rules-code-style.md} +0 -0
  74. /package/.ai/{rules-skill/ecosystem.md → rules-ecosystem.md} +0 -0
  75. /package/.ai/{rules-skill/error-system.md → rules-error-system.md} +0 -0
  76. /package/.ai/{rules-skill/hooks.md → rules-hooks.md} +0 -0
  77. /package/.ai/{rules-skill/philosophy.md → rules-philosophy.md} +0 -0
  78. /package/.ai/{rules-skill/testing.md → rules-testing.md} +0 -0
@@ -14,7 +14,7 @@ const {
14
14
  const { WRONG_TYPE_DEFINITION, } = ErrorsTypes;
15
15
 
16
16
  import {
17
- _getProps, Props
17
+ _getProps, Props
18
18
  } from '../types/Props';
19
19
  import type { MnemonicaConstructor } from '../../types';
20
20
 
@@ -39,22 +39,22 @@ const getTypeChecker = (TypeName: string) => {
39
39
  return false;
40
40
  }
41
41
 
42
-
42
+
43
43
  if (!instance!.constructor) {
44
44
  return false;
45
45
  }
46
-
46
+
47
47
  // @ts-expect-error I'm too lazy for that
48
48
  if (Reflect.getPrototypeOf(instance).constructor.name === 'Promise') {
49
49
  // if ( instance instanceof Promise ) {
50
-
50
+
51
51
  // @ts-expect-error I'm too lazy for that
52
52
  const checkResult = instance[ SymbolConstructorName ] === TypeName;
53
53
  return checkResult;
54
54
  }
55
55
 
56
56
  const constructors = collectConstructors(instance) as Record<string, new () => unknown>;
57
-
57
+
58
58
  const constructorResult = constructors[ TypeName ] || false;
59
59
  return constructorResult;
60
60
 
@@ -62,6 +62,19 @@ const getTypeChecker = (TypeName: string) => {
62
62
  return seeker;
63
63
  };
64
64
 
65
+ const typeCheckerCache = new Map<string, unknown>();
66
+
67
+ // getTypeChecker builds a fresh closure per call, and instanceof
68
+ // checks run per access — so the closures are cached per TypeName
69
+ const getCachedTypeChecker = (TypeName: string) => {
70
+ let checker = typeCheckerCache.get(TypeName);
71
+ if (!checker) {
72
+ checker = getTypeChecker(TypeName);
73
+ typeCheckerCache.set(TypeName, checker);
74
+ }
75
+ return checker;
76
+ };
77
+
65
78
  const getTypeSplitPath = (path: string) => {
66
79
  const split = path
67
80
  // beautifull names
@@ -176,7 +189,7 @@ const findSubTypeFromParent = (instance: parentSub | object | undefined, subType
176
189
  const props = _getProps(instance) as Props;
177
190
 
178
191
  if (props) {
179
- if(props.__type__.subtypes.has(subType)) {
192
+ if (props.__type__.subtypes.has(subType)) {
180
193
  const _subtype = props.__type__.subtypes.get(subType);
181
194
  subtype = _subtype;
182
195
  } else {
@@ -186,7 +199,7 @@ const findSubTypeFromParent = (instance: parentSub | object | undefined, subType
186
199
  );
187
200
  }
188
201
  }
189
-
202
+
190
203
  // @ts-expect-error I'm too lazy for that
191
204
  return subtype;
192
205
  };
@@ -199,12 +212,12 @@ const findSubTypeFromParent = (instance: parentSub | object | undefined, subType
199
212
  // accordingly to the gist from here:
200
213
  // https://gist.github.com/wentout/ea3afe9c822a6b6ef32f9e4f3e98b1ba
201
214
  const isClass = (fn: ConstructHandler) => {
202
-
215
+
216
+ /*
203
217
  const str = String(fn);
204
218
  const result = str.indexOf('class ') === 0;
205
219
  return result;
206
220
 
207
- /*
208
221
 
209
222
  // not necessary to check fn for typeof
210
223
  // because of other checks made before
@@ -234,6 +247,10 @@ const isClass = (fn: ConstructHandler) => {
234
247
  return Reflect.getOwnPropertyDescriptor(fn, 'prototype')!.writable === false;
235
248
 
236
249
  */
250
+
251
+ const protoDesc = Reflect.getOwnPropertyDescriptor(fn, 'prototype');
252
+ const result = protoDesc !== undefined && protoDesc.writable === false;
253
+ return result;
237
254
  };
238
255
 
239
256
  const makeErrorModificatorType = (
@@ -304,6 +321,7 @@ const TypesUtils = {
304
321
  isClass,
305
322
  checkProto,
306
323
  getTypeChecker,
324
+ getCachedTypeChecker,
307
325
  getTypeSplitPath,
308
326
  getExistentAsyncStack,
309
327
  checkTypeName,
@@ -4,6 +4,8 @@ import type {
4
4
  _Internal_TC_,
5
5
  CreateTypesCollectionFunction,
6
6
  TypesCollection,
7
+ TypeClass,
8
+ IDEF,
7
9
  hooksOpts,
8
10
  hook
9
11
  } from '../../types';
@@ -22,7 +24,7 @@ const {
22
24
 
23
25
  // here is TypesCollection.define() method
24
26
  import {
25
- define, lookup, type TypesMap
27
+ define, lazy, lookup, type TypesMap, type LazyTypeGetter
26
28
  } from '../../api/types';
27
29
 
28
30
  import * as hooksAPI from '../../api/hooks';
@@ -154,9 +156,11 @@ odp(
154
156
  constructHandlerOrConfig?: CallableFunction | object,
155
157
  config?: object
156
158
  ) {
157
- // this - define function of mnemonica interface
159
+ // pass `result` itself as the stack-capture boundary (StackBoundary):
160
+ // it is a real callable on the stack, so captureStackTrace
161
+ // truncates at the user's call site instead of keeping internal frames
158
162
  const defineResult = define.call(
159
- this as unknown,
163
+ result,
160
164
  subtypes as TypesMap,
161
165
  TypeOrTypeName,
162
166
  constructHandlerOrConfig,
@@ -170,6 +174,80 @@ odp(
170
174
  }
171
175
  );
172
176
 
177
+ odp(
178
+ TypesCollection.prototype,
179
+ 'lazy',
180
+ {
181
+ get (this: { subtypes: Map<string, object> }) {
182
+ const { subtypes } = this;
183
+ const result = function (
184
+ this: CallableFunction,
185
+ arg1: string | CallableFunction,
186
+ arg2?: CallableFunction | object,
187
+ arg3?: object
188
+ ) {
189
+ let name: string | undefined;
190
+ let getter: LazyTypeGetter;
191
+ let config: object | undefined;
192
+ if (typeof arg1 === 'string') {
193
+ name = arg1;
194
+ getter = arg2 as LazyTypeGetter;
195
+ config = arg3;
196
+ } else {
197
+ getter = arg1 as LazyTypeGetter;
198
+ config = arg2 as object;
199
+ }
200
+ let lazyResult: TypeClass;
201
+ // same as in `define` above: pass `result` itself as the
202
+ // stack-capture boundary, not the collection object
203
+ if (name) {
204
+ lazyResult = lazy.call(
205
+ result,
206
+ subtypes as TypesMap,
207
+ name,
208
+ getter as LazyTypeGetter,
209
+ config
210
+ );
211
+ } else {
212
+ lazyResult = lazy.call(
213
+ result,
214
+ subtypes as TypesMap,
215
+ getter as LazyTypeGetter,
216
+ config
217
+ );
218
+ }
219
+ return lazyResult;
220
+ };
221
+ return result;
222
+ },
223
+ enumerable : true
224
+ }
225
+ );
226
+
227
+ odp(
228
+ TypesCollection.prototype,
229
+ 'decorate',
230
+ {
231
+ get (this: TypesCollection) {
232
+ const self = this;
233
+ const result = function (config?: object) {
234
+ const decorator = function (cstr: CallableFunction) {
235
+ const { name } = cstr;
236
+ const defineResult = self.define(
237
+ name,
238
+ cstr as IDEF<object>,
239
+ config
240
+ );
241
+ return defineResult;
242
+ };
243
+ return decorator;
244
+ };
245
+ return result;
246
+ },
247
+ enumerable : true
248
+ }
249
+ );
250
+
173
251
  odp(
174
252
  TypesCollection.prototype,
175
253
  'lookup',
package/src/index.ts CHANGED
@@ -13,9 +13,14 @@ import type {
13
13
  DecoratedClass,
14
14
  TypeClass,
15
15
  TypeAbsorber,
16
+ LazyAbsorber,
16
17
  MnemonicaModule,
17
18
  InstanceResult,
18
- Merge
19
+ Merge,
20
+ TypeLookup,
21
+ LookupResult,
22
+ RegistryHolderBase,
23
+ StoredConstructor
19
24
  } from './types';
20
25
 
21
26
  import TypesUtils from './api/utils/index';
@@ -26,6 +31,7 @@ export const {
26
31
 
27
32
  export type {
28
33
  IDEF,
34
+ LazyDef,
29
35
  TypeConstructor,
30
36
  TypeConstructorBase,
31
37
  Proto,
@@ -43,6 +49,8 @@ export type {
43
49
  TypeAbsorber,
44
50
  TypesCollection,
45
51
  TypeLookup,
52
+ LookupResult,
53
+ RegistryOf,
46
54
  MnemonicaModule,
47
55
  } from './types';
48
56
 
@@ -95,6 +103,28 @@ function checkThis(pointer: typeof mnemonica | typeof exports | unknown): boolea
95
103
 
96
104
 
97
105
  // Define function using TypeAbsorber interface with proper type casting
106
+ export function define <
107
+ Reg extends object,
108
+ Parent extends object,
109
+ Path extends string,
110
+ const Name extends string,
111
+ N extends object,
112
+ Args extends unknown[],
113
+ F extends Proto<Parent, N> = Proto<Parent, N>,
114
+ ChildPath extends string = Path extends '' ? Name : `${Path}.${Name}`
115
+ >(
116
+ // explicit-source form: define(source, name, handler, config?)
117
+ // mirrors RegistryHolderBase.define; semantics follow the source object
118
+ source: RegistryHolderBase<Reg, Parent, Path>,
119
+ TypeName: Name,
120
+ constructHandler?: IDEF<N, Args>,
121
+ config?: constructorOptions
122
+ ): IDefinitorInstance<
123
+ F,
124
+ InstanceResult<F>,
125
+ Reg & Record<ChildPath, StoredConstructor<F, ChildPath>>,
126
+ ChildPath
127
+ >;
98
128
  export function define <
99
129
  T extends object,
100
130
  P extends object = object,
@@ -107,30 +137,141 @@ export function define <
107
137
  // Allow both strict IDEF and more flexible function signatures
108
138
  constructHandler?: IDEF<T> | CallableFunction | NewableFunction | object | boolean,
109
139
  config?: constructorOptions,
140
+ ): R;
141
+ export function define <
142
+ T extends object,
143
+ P extends object = object,
144
+ N extends Proto<P, T> = Proto<P, T>,
145
+ R extends IDefinitorInstance<N> = IDefinitorInstance<N>,
146
+ >(
147
+ this: unknown,
148
+ TypeNameOrSource?: string | CallableFunction | NewableFunction | RegistryHolderBase<object, object, string>,
149
+ constructHandlerOrName?: IDEF<T> | CallableFunction | NewableFunction | object | boolean | string,
150
+ configOrHandler?: constructorOptions | IDEF<T> | CallableFunction | NewableFunction | object | boolean,
151
+ config?: constructorOptions,
110
152
  ): R {
153
+
154
+ // explicit-source form: define(source, name, handler, config?)
155
+ // the source may be a TypesCollection (object) or a TypeProxy (function),
156
+ // so detection is: has a callable .define AND the next arg is a string name
157
+ const mayBeSource = (
158
+ (typeof TypeNameOrSource === 'object' && TypeNameOrSource !== null) ||
159
+ typeof TypeNameOrSource === 'function'
160
+ ) && typeof (TypeNameOrSource as { define?: unknown }).define === 'function';
161
+ if (mayBeSource && typeof constructHandlerOrName === 'string') {
162
+ const source = TypeNameOrSource as unknown as { define: TypeAbsorber };
163
+ const sourceDefineResult = source.define(
164
+ constructHandlerOrName,
165
+ configOrHandler as IDEF<T>,
166
+ config
167
+ ) as unknown as R;
168
+ return sourceDefineResult;
169
+ }
170
+
111
171
  const types = checkThis(this) ? defaultTypes : this || defaultTypes;
112
172
  // Type assertion needed because TypesCollectionProxy is a Proxy
113
173
  const defineResult = (types as { define: TypeAbsorber })
114
174
  .define(
115
- TypeName as string,
116
- constructHandler as IDEF<T>,
117
- config
175
+ TypeNameOrSource as string,
176
+ constructHandlerOrName as IDEF<T>,
177
+ configOrHandler as constructorOptions
118
178
  ) as unknown as R;
119
179
  return defineResult;
120
180
  }
121
181
 
182
+ export function lazy<T extends object>(
183
+ // explicit-source named form: lazy(source, TypeName, getter, config?)
184
+ source: RegistryHolderBase<object, object, string>,
185
+ TypeName: string,
186
+ getter: () => IDEF<T>,
187
+ config?: constructorOptions
188
+ ): IDefinitorInstance<T>;
189
+ export function lazy<T extends object>(
190
+ // explicit-source unnamed form: lazy(source, getter, config?)
191
+ source: RegistryHolderBase<object, object, string>,
192
+ getter: () => IDEF<T>,
193
+ config?: constructorOptions
194
+ ): IDefinitorInstance<T>;
195
+ export function lazy<T extends object>(
196
+ // named form: lazy(TypeName, getter, config?)
197
+ this: unknown,
198
+ TypeName: string,
199
+ getter: () => IDEF<T>,
200
+ config?: constructorOptions
201
+ ): IDefinitorInstance<T>;
202
+ export function lazy<T extends object>(
203
+ // unnamed form: lazy(getter, config?)
204
+ this: unknown,
205
+ getter: () => IDEF<T>,
206
+ config?: constructorOptions
207
+ ): IDefinitorInstance<T>;
208
+ export function lazy(
209
+ this: unknown,
210
+ arg1: unknown,
211
+ arg2?: unknown,
212
+ arg3?: unknown,
213
+ arg4?: unknown
214
+ ): unknown {
215
+
216
+ // explicit-source form: lazy(source, name?, getter, config?)
217
+ // the source may be a TypesCollection (object) or a TypeProxy (function),
218
+ // so detection is: has a callable .lazy
219
+ const mayBeSource = (
220
+ (typeof arg1 === 'object' && arg1 !== null) ||
221
+ typeof arg1 === 'function'
222
+ ) && typeof (arg1 as { lazy?: unknown }).lazy === 'function';
223
+ if (mayBeSource) {
224
+ const source = arg1 as unknown as { lazy: LazyAbsorber };
225
+ const sourceLazyResult = source.lazy(
226
+ arg2 as string | (() => IDEF<object>),
227
+ arg3 as (() => IDEF<object>) | constructorOptions,
228
+ arg4 as constructorOptions
229
+ ) as unknown;
230
+ return sourceLazyResult;
231
+ }
232
+
233
+ const types = checkThis(this) ? defaultTypes : this || defaultTypes;
234
+ // Type assertion needed because TypesCollectionProxy is a Proxy
235
+ const lazyResult = (types as { lazy: LazyAbsorber })
236
+ .lazy(
237
+ arg1 as string | (() => IDEF<object>),
238
+ arg2 as (() => IDEF<object>) | constructorOptions,
239
+ arg3 as constructorOptions
240
+ ) as unknown;
241
+ return lazyResult;
242
+ }
243
+
122
244
  export function lookup<const K extends keyof TypeRegistry>(
123
245
  this: unknown,
124
246
  TypeNestedPath: K
125
247
  ): TypeRegistry[K];
248
+ export function lookup<Reg extends object, const K extends keyof Reg & string>(
249
+ // explicit-source form: lookup(source, path)
250
+ // resolves against the registry carried by a builder/collection value
251
+ source: { lookup: TypeLookup<Reg> },
252
+ TypeNestedPath: K
253
+ ): LookupResult<Reg, K>;
126
254
  export function lookup(this: unknown, TypeNestedPath: string): TypeClass | undefined;
127
255
  export function lookup(
128
- this: unknown,
256
+ source: { lookup: (path: string) => TypeClass | undefined },
129
257
  TypeNestedPath: string
130
- ): TypeClass | undefined {
258
+ ): TypeClass | undefined;
259
+ export function lookup(
260
+ this: unknown,
261
+ arg1: unknown,
262
+ arg2?: unknown
263
+ ): unknown {
264
+
265
+ // explicit-source form: lookup(source, path)
266
+ if (typeof arg1 !== 'string' && typeof arg2 === 'string') {
267
+ const source = arg1 as { lookup: (path: string) => TypeClass | undefined };
268
+ const sourceResult = source.lookup(arg2);
269
+ return sourceResult;
270
+ }
271
+
131
272
  const types = checkThis(this) ? defaultTypes : this || defaultTypes;
132
273
  // Type assertion needed because TypesCollectionProxy is a Proxy
133
- const lookupResult = (types as { lookup: (path: string) => TypeClass | undefined }).lookup(TypeNestedPath);
274
+ const lookupResult = (types as { lookup: (path: string) => TypeClass | undefined }).lookup(arg1 as string);
134
275
  return lookupResult;
135
276
  }
136
277
 
@@ -259,6 +400,7 @@ export const registerHook = function <T extends Constructor<T>>(
259
400
  export const mnemonica = Object.entries({
260
401
 
261
402
  define,
403
+ lazy,
262
404
  lookup,
263
405
  apply,
264
406
  call,
@@ -293,6 +435,7 @@ import * as api from './api';
293
435
 
294
436
  export const {
295
437
  define: _define,
438
+ lazy: _lazy,
296
439
  lookup: _lookup
297
440
  } = api.types;
298
441
 
@@ -13,6 +13,10 @@ export type PropsType = Record<string, unknown>;
13
13
  // supply Args to get typed constructor params: IDEF<MyType, [string, number]>
14
14
  export type IDEF<T, Args extends unknown[] = unknown[]> = { new(): T } | { (this: T, ...args: Args): void };
15
15
 
16
+ // Lazy getter type - a zero-arg factory that returns a constructor.
17
+ // Used by the explicit .lazy() API.
18
+ export type LazyDef<T, Args extends unknown[] = unknown[]> = () => IDEF<T, Args>;
19
+
16
20
  // Error message types - all error messages are strings
17
21
  export type ErrorMessageKey =
18
22
  | 'BASE_ERROR_MESSAGE'
@@ -26,8 +30,8 @@ export type ErrorMessageKey =
26
30
  | 'MISSING_HOOK_CALLBACK'
27
31
  | 'MISSING_CALLBACK_ARGUMENT'
28
32
  | 'OPTIONS_ERROR'
29
- | 'WRONG_STACK_CLEANER';
30
- // | 'TYPENAME_MUST_BE_A_STRING'
33
+ | 'WRONG_STACK_CLEANER'
34
+ | 'TYPENAME_MUST_BE_A_STRING';
31
35
  // | 'FLOW_CHECKER_REDEFINITION'
32
36
 
33
37
  // Error messages object type
@@ -335,6 +339,20 @@ export interface TypeLookup<T extends object = GlobalRegistry> extends CallableF
335
339
  (this: unknown, TypeNestedPath: string): TypeClass | undefined;
336
340
  }
337
341
 
342
+ // Extracts the accumulated registry map from a builder-chain value
343
+ // (IDefinitorInstance, TypesCollection, or MnemonicaModule). Enables the
344
+ // one-line bridge that merges a builder's local registry into the global
345
+ // TypeRegistry without Tactica:
346
+ //
347
+ // declare module 'mnemonica' {
348
+ // interface TypeRegistry extends RegistryOf<typeof App> {}
349
+ // }
350
+ export type RegistryOf<T> =
351
+ T extends IDefinitorInstance<object, InstanceResult<object>, infer Registry, string> ? Registry
352
+ : T extends TypesCollection<infer Registry, object, string> ? Registry
353
+ : T extends MnemonicaModule<infer Registry> ? Registry
354
+ : never;
355
+
338
356
  // Relative keys of a registry under a given dotted path. For a root path (`''`)
339
357
  // this is all string keys; for a non-empty path it is the tail after `${Path}.`.
340
358
  export type RelativeKeys<
@@ -567,6 +585,31 @@ export interface RegistryHolderBase<
567
585
  T & Record<ChildPath, StoredConstructor<F, ChildPath>>,
568
586
  ChildPath
569
587
  >;
588
+
589
+ // Explicit lazy getter: .lazy(() => Constructor, config?)
590
+ lazy<SubType extends object>(
591
+ this: RegistryHolderBase<T, Parent, Path>,
592
+ getter: LazyDef<SubType>,
593
+ config?: constructorOptions
594
+ ): IDefinitorInstance<SubType>;
595
+
596
+ // Named lazy getter: .lazy(TypeName, () => Constructor, config?)
597
+ lazy<
598
+ const Name extends string,
599
+ N extends object,
600
+ F extends Proto<Parent, N> = Proto<Parent, N>,
601
+ ChildPath extends string = Path extends '' ? Name : `${Path}.${Name}`
602
+ >(
603
+ this: RegistryHolderBase<T, Parent, Path>,
604
+ TypeName: Name,
605
+ getter: LazyDef<N>,
606
+ config?: constructorOptions
607
+ ): IDefinitorInstance<
608
+ F,
609
+ InstanceResult<F>,
610
+ T & Record<ChildPath, StoredConstructor<F, ChildPath>>,
611
+ ChildPath
612
+ >;
570
613
  }
571
614
 
572
615
  // Full registry holder - adds typed .lookup() for collections that are not
@@ -610,6 +653,8 @@ export interface IDefinitorInstance<
610
653
 
611
654
  lookup: TypeLookup<Registry>;
612
655
 
656
+ decorate: (config?: constructorOptions) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
657
+
613
658
  registerHook(hookType: hooksTypes, cb: hook): void;
614
659
 
615
660
  subtypes: SubtypesMap;
@@ -638,6 +683,47 @@ export interface TypeAbsorber extends CallableFunction {
638
683
  ): IDefinitorInstance<T>;
639
684
  }
640
685
 
686
+ // Lazy absorber function type - used for explicit lazy getter definitions
687
+ export interface LazyAbsorber extends CallableFunction {
688
+ <T extends object>(
689
+ this: unknown,
690
+ getter: LazyDef<T>,
691
+ config?: constructorOptions
692
+ ): IDefinitorInstance<T>;
693
+ <T extends object>(
694
+ this: unknown,
695
+ TypeName: string,
696
+ getter: LazyDef<T>,
697
+ config?: constructorOptions
698
+ ): IDefinitorInstance<T>;
699
+ <T extends object>(
700
+ this: unknown,
701
+ arg1: string | LazyDef<T>,
702
+ arg2: LazyDef<T> | constructorOptions,
703
+ arg3?: constructorOptions
704
+ ): IDefinitorInstance<T>;
705
+ <T extends object>(
706
+ this: unknown,
707
+ source: RegistryHolderBase<object, object, string>,
708
+ getter: LazyDef<T>,
709
+ config?: constructorOptions
710
+ ): IDefinitorInstance<T>;
711
+ <T extends object>(
712
+ this: unknown,
713
+ source: RegistryHolderBase<object, object, string>,
714
+ TypeName: string,
715
+ getter: LazyDef<T>,
716
+ config?: constructorOptions
717
+ ): IDefinitorInstance<T>;
718
+ <T extends object>(
719
+ this: unknown,
720
+ source: RegistryHolderBase<object, object, string>,
721
+ arg1: string | LazyDef<T>,
722
+ arg2: LazyDef<T> | constructorOptions,
723
+ arg3?: constructorOptions
724
+ ): IDefinitorInstance<T>;
725
+ }
726
+
641
727
  // TypesCollection interface for createTypesCollection
642
728
  // This represents the actual return type of createTypesCollection
643
729
  export interface TypesCollection<
@@ -647,6 +733,16 @@ export interface TypesCollection<
647
733
  >
648
734
  extends RegistryHolder<T, Parent, Path>, Hookable {
649
735
  subtypes: SubtypesMap;
736
+
737
+ // Decorator factory: .decorate() or .decorate(config)
738
+ // Returns a class decorator that registers the class in this collection's registry.
739
+ decorate(
740
+ config?: constructorOptions
741
+ ): <U extends Constructor<object>>(cstr: U) => DecoratedClass<
742
+ U,
743
+ T & Record<ConstructorName<U>, StoredConstructor<InstanceType<U>, ConstructorName<U>>>
744
+ >;
745
+
650
746
  [key: string]: unknown;
651
747
  }
652
748
 
@@ -688,15 +784,25 @@ export interface TypeDescriptorDefine extends CallableFunction {
688
784
  ): TypeClass;
689
785
  }
690
786
 
787
+ export interface TypeDescriptorLazy extends CallableFunction {
788
+ (
789
+ getter: CallableFunction,
790
+ config?: object
791
+ ): TypeClass;
792
+ }
793
+
691
794
  export interface TypeDescriptorLookup extends CallableFunction {
692
795
  (TypeNestedPath: string): TypeClass | undefined;
693
796
  }
694
797
 
695
798
  export type TypeDescriptorInstance = {
696
799
  define: TypeDescriptorDefine;
800
+ lazy: TypeDescriptorLazy;
801
+ decorate: (config?: constructorOptions) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U>;
697
802
  lookup: TypeDescriptorLookup;
698
803
  subtypes: Map<string, object>;
699
804
  TypeName: string;
805
+ collection: CollectionDef;
700
806
  };
701
807
 
702
808
  // Constructor type for decorate function
@@ -708,18 +814,22 @@ export type ConstructorName<T extends Constructor<object>> =
708
814
 
709
815
  // Decorated class type - includes call signature for decorator pattern and
710
816
  // aligns with `IDefinitorInstance` so decorated classes expose the same
711
- // constructor/lookup surface as builder-created types. It still relies on the
712
- // global `TypeRegistry` for typed lookups because decorators cannot carry a
713
- // local accumulating registry across independent class declarations.
714
- export type DecoratedClass<T extends Constructor<object>> =
817
+ // constructor/lookup surface as builder-created types.
818
+ // `Registry` defaults to the global `TypeRegistry`; for custom collections it
819
+ // is the collection's local registry so `lookup()` and nested `.decorate()`
820
+ // stay inside that collection.
821
+ export type DecoratedClass<
822
+ T extends Constructor<object>,
823
+ Registry extends object = TypeRegistry
824
+ > =
715
825
  T &
716
826
  Omit<
717
- IDefinitorInstance<InstanceType<T>, InstanceType<T>, TypeRegistry, ConstructorName<T>>,
827
+ IDefinitorInstance<InstanceType<T>, InstanceType<T>, Registry, ConstructorName<T>>,
718
828
  'define' | 'lookup'
719
829
  > &
720
- (<U extends Constructor<object>>(target: U) => DecoratedClass<U>) & {
830
+ (<U extends Constructor<object>>(target: U) => DecoratedClass<U, Registry>) & {
721
831
  define: TypeAbsorber;
722
- lookup: TypeLookup<TypeRegistry>;
832
+ lookup: TypeLookup<Registry>;
723
833
  };
724
834
 
725
835
  // Helper: merge parent entity (E) and child constructor instance (T) into a
@@ -794,7 +904,7 @@ export interface MnemonicaModule<Registry extends object = {}>
794
904
  decorate: <T extends Constructor<object> | constructorOptions | undefined = undefined>(
795
905
  target?: T,
796
906
  config?: constructorOptions
797
- ) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U>;
907
+ ) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
798
908
  registerHook: <T extends object>(Constructor: IDEF<T>, hookType: hooksTypes, cb: hook) => void;
799
909
 
800
910
  // Descriptors