mnemonica 1.3.3 → 1.3.6
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/FOR_HUMANS.md +4 -4
- package/README.md +2 -2
- package/SKILL.md +12 -2
- package/build/api/types/InstanceCreator.js +10 -9
- package/build/api/types/compileNewModificatorFunctionBody.d.ts +1 -0
- package/build/api/types/compileNewModificatorFunctionBody.js +19 -1
- package/build/api/types/index.js +5 -2
- package/build/constants/index.js +3 -3
- package/build/index.d.ts +4 -4
- package/build/index.js +1 -1
- package/build/types/index.d.ts +19 -15
- package/build/types/index.js +1 -1
- package/docs/async-constructors.md +62 -11
- package/docs/typed-lookup.md +111 -0
- package/package.json +135 -133
- package/src/api/types/InstanceCreator.ts +17 -11
- package/src/api/types/compileNewModificatorFunctionBody.ts +34 -0
- package/src/api/types/index.ts +19 -4
- package/src/constants/index.ts +5 -3
- package/src/index.ts +16 -4
- package/src/types/index.ts +55 -19
package/src/api/types/index.ts
CHANGED
|
@@ -72,7 +72,10 @@ const {
|
|
|
72
72
|
import * as hooksApi from '../hooks';
|
|
73
73
|
import { TypeProxy } from './TypeProxy';
|
|
74
74
|
|
|
75
|
-
import compileNewModificatorFunctionBody, {
|
|
75
|
+
import compileNewModificatorFunctionBody, {
|
|
76
|
+
classifyConstructHandler,
|
|
77
|
+
ConstructHandler
|
|
78
|
+
} from './compileNewModificatorFunctionBody';
|
|
76
79
|
|
|
77
80
|
import TypesUtils, { CreationHandler } from '../utils';
|
|
78
81
|
const {
|
|
@@ -262,9 +265,9 @@ TypeDescriptor.prototype.lookup = function (
|
|
|
262
265
|
// collection, e.g. AdminType.lookup('UserType.AdminType'),
|
|
263
266
|
// which is what the TypeLookup<Registry> type contract promises
|
|
264
267
|
const collection = this.collection as CollectionDef;
|
|
265
|
-
// the
|
|
266
|
-
//
|
|
267
|
-
const rootResult = collection.lookup(TypeNestedPath)
|
|
268
|
+
// CollectionDef.lookup is the runtime contract (CollectionLookup), so a
|
|
269
|
+
// plain string path yields TypeClass | undefined with no cast
|
|
270
|
+
const rootResult = collection.lookup(TypeNestedPath);
|
|
268
271
|
return rootResult;
|
|
269
272
|
};
|
|
270
273
|
|
|
@@ -406,6 +409,18 @@ const createFromDirectHandler = function (
|
|
|
406
409
|
(typeof handler.prototype === 'object')
|
|
407
410
|
) ? handler.prototype : getDefaultPrototype();
|
|
408
411
|
|
|
412
|
+
// reject the unsupported handler shapes (generators, sync arrows,
|
|
413
|
+
// shorthand methods, bound functions) BEFORE the type is created, so
|
|
414
|
+
// a rejected define leaves nothing registered. Skipped when the name
|
|
415
|
+
// is taken: the TypeDescriptor below throws the canonical
|
|
416
|
+
// ALREADY_DECLARED, which wins over the shape error
|
|
417
|
+
if ( !target.has( name ) ) {
|
|
418
|
+
classifyConstructHandler(
|
|
419
|
+
name,
|
|
420
|
+
handler as ConstructHandler
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
|
|
409
424
|
const result = new TypeDescriptor(
|
|
410
425
|
defineOrigin,
|
|
411
426
|
target,
|
package/src/constants/index.ts
CHANGED
|
@@ -73,10 +73,12 @@ const defaultOptions = {
|
|
|
73
73
|
},
|
|
74
74
|
|
|
75
75
|
// await new Constructor()
|
|
76
|
-
// must
|
|
76
|
+
// with unchain:false the construction must resolve to its instance;
|
|
77
|
+
// with unchain:true a non-object resolution DROPS the chain and that
|
|
78
|
+
// value is the result ("unchain on await")
|
|
77
79
|
// optional ./issues/106
|
|
78
|
-
get
|
|
79
|
-
return
|
|
80
|
+
get unchain () {
|
|
81
|
+
return false;
|
|
80
82
|
},
|
|
81
83
|
|
|
82
84
|
} as Record<string, unknown>;
|
package/src/index.ts
CHANGED
|
@@ -19,9 +19,9 @@ import type {
|
|
|
19
19
|
InstanceResult,
|
|
20
20
|
Merge,
|
|
21
21
|
TypeLookup,
|
|
22
|
-
LookupResult,
|
|
23
22
|
RegistryHolderBase,
|
|
24
|
-
|
|
23
|
+
RegistryEntry,
|
|
24
|
+
LookedUpConstructor
|
|
25
25
|
} from './types';
|
|
26
26
|
|
|
27
27
|
import TypesUtils from './api/utils/index';
|
|
@@ -55,6 +55,12 @@ export type {
|
|
|
55
55
|
LookupResult,
|
|
56
56
|
RegistryOf,
|
|
57
57
|
MnemonicaModule,
|
|
58
|
+
// Deliberate public vocabulary for builder-mode consumers (TS 6
|
|
59
|
+
// declaration emit names ONLY entry exports; these three stand in
|
|
60
|
+
// for the internal registry/lookup machinery):
|
|
61
|
+
RegistryEntry,
|
|
62
|
+
LookedUpConstructor,
|
|
63
|
+
LookedUpInstance,
|
|
58
64
|
} from './types';
|
|
59
65
|
|
|
60
66
|
/**
|
|
@@ -75,6 +81,12 @@ export type {
|
|
|
75
81
|
* Once augmented, `lookup('MyType')` and `utils.parent(instance, 'MyType')`
|
|
76
82
|
* resolve keys against this registry. Unaugmented keys fall back to the broad
|
|
77
83
|
* `TypeClass | undefined` return type.
|
|
84
|
+
*
|
|
85
|
+
* Internal on purpose: if you are here from a TS2883 error
|
|
86
|
+
* ("cannot be named without a reference to 'GlobalRegistry'"), the project
|
|
87
|
+
* exports free define() results without merging them into this registry.
|
|
88
|
+
* The error is the guard rail, not a missing export — see
|
|
89
|
+
* docs/typed-lookup.md, "Declaration emit on TypeScript 6".
|
|
78
90
|
*/
|
|
79
91
|
export interface TypeRegistry {
|
|
80
92
|
// Intentionally empty. Augment this interface via declaration merging
|
|
@@ -136,7 +148,7 @@ export function define <
|
|
|
136
148
|
): IDefinitorInstance<
|
|
137
149
|
F,
|
|
138
150
|
InstanceResult<F>,
|
|
139
|
-
Reg & Record<ChildPath,
|
|
151
|
+
Reg & Record<ChildPath, RegistryEntry<F, ChildPath>>,
|
|
140
152
|
ChildPath
|
|
141
153
|
>;
|
|
142
154
|
export function define <
|
|
@@ -264,7 +276,7 @@ export function lookup<Reg extends object, const K extends keyof Reg & string>(
|
|
|
264
276
|
// resolves against the registry carried by a builder/collection value
|
|
265
277
|
source: { lookup: TypeLookup<Reg> },
|
|
266
278
|
TypeNestedPath: K
|
|
267
|
-
):
|
|
279
|
+
): LookedUpConstructor<Reg, K>;
|
|
268
280
|
export function lookup(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
269
281
|
export function lookup(
|
|
270
282
|
source: { lookup: (path: string) => TypeClass | undefined },
|
package/src/types/index.ts
CHANGED
|
@@ -251,8 +251,10 @@ export type constructorOptions = {
|
|
|
251
251
|
// during the process of instance creation
|
|
252
252
|
submitStack?: boolean,
|
|
253
253
|
// await new Constructor()
|
|
254
|
-
// must
|
|
255
|
-
|
|
254
|
+
// with false (default) the construction must resolve to its instance;
|
|
255
|
+
// with true a non-object resolution DROPS the chain and that value is
|
|
256
|
+
// the result ("unchain on await")
|
|
257
|
+
unchain?: boolean,
|
|
256
258
|
// Force class mode (auto-detected by default)
|
|
257
259
|
asClass?: boolean,
|
|
258
260
|
};
|
|
@@ -278,10 +280,18 @@ export type TypeDef = {
|
|
|
278
280
|
[Symbol.hasInstance]: (instance: object) => boolean;
|
|
279
281
|
};
|
|
280
282
|
|
|
283
|
+
// Runtime lookup contract of a collection: a dotted path in, a registered
|
|
284
|
+
// TypeClass (or undefined) out. Internal code uses this, not the public
|
|
285
|
+
// TypeLookup — with the default GlobalRegistry (a string index) a plain
|
|
286
|
+
// `string` path would match TypeLookup's typed overload and need a cast back.
|
|
287
|
+
export interface CollectionLookup extends CallableFunction {
|
|
288
|
+
(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
289
|
+
}
|
|
290
|
+
|
|
281
291
|
// Collection definition
|
|
282
292
|
export type CollectionDef = Hookable & {
|
|
283
293
|
define: TypeAbsorber;
|
|
284
|
-
lookup:
|
|
294
|
+
lookup: CollectionLookup;
|
|
285
295
|
subtypes: SubtypesMap;
|
|
286
296
|
[key: string]: unknown;
|
|
287
297
|
};
|
|
@@ -290,6 +300,10 @@ export type CollectionDef = Hookable & {
|
|
|
290
300
|
// Global registry shape used as the default for TypeLookup. It combines any
|
|
291
301
|
// augmented TypeRegistry entries with a broad string index signature so the
|
|
292
302
|
// generic TypeLookup constraint is satisfied.
|
|
303
|
+
// Internal on purpose: if you are here from a TS2883 error ("cannot be
|
|
304
|
+
// named without a reference to 'GlobalRegistry'"), the consumer exports free
|
|
305
|
+
// define() results without a TypeRegistry merge — see docs/typed-lookup.md,
|
|
306
|
+
// "Declaration emit on TypeScript 6". Do not re-export this.
|
|
293
307
|
export type GlobalRegistry = TypeRegistry & Record<string, TypeConstructorBase>;
|
|
294
308
|
|
|
295
309
|
// Extract the instance type returned by a mnemonica-compatible constructor.
|
|
@@ -309,9 +323,7 @@ export type SubTypeConstructors<
|
|
|
309
323
|
Path extends string
|
|
310
324
|
> = {
|
|
311
325
|
[K in keyof Registry as K extends `${Path}.${infer Child}` ? Child : never]:
|
|
312
|
-
|
|
313
|
-
lookup: NestedTypeLookup<Registry, K & string>;
|
|
314
|
-
};
|
|
326
|
+
LookedUpConstructor<Registry, K & string, true>;
|
|
315
327
|
};
|
|
316
328
|
|
|
317
329
|
// Reconstruct a constructor with a new instance type while preserving every
|
|
@@ -367,23 +379,45 @@ export type AugmentedConstructor<
|
|
|
367
379
|
CallConstructs extends boolean = false
|
|
368
380
|
> = ReplaceConstructorInstance<
|
|
369
381
|
Registry[Path],
|
|
370
|
-
|
|
382
|
+
LookedUpInstance<Registry, Path>,
|
|
371
383
|
CallConstructs
|
|
372
384
|
>;
|
|
373
385
|
|
|
386
|
+
// Instance type produced by a typed lookup (public vocabulary): the
|
|
387
|
+
// constructor's own instance shape plus child-constructor properties for
|
|
388
|
+
// every registered subtype. Referencing this name — instead of the internal
|
|
389
|
+
// WithSubTypes alias — keeps consumer declaration emit portable (TS names
|
|
390
|
+
// alias instantiations in emit; only entry-exported names are nameable).
|
|
391
|
+
export type LookedUpInstance<
|
|
392
|
+
Registry extends object,
|
|
393
|
+
Path extends string
|
|
394
|
+
> = WithSubTypes<ExtractConstructorInstance<Registry[Path & keyof Registry]>, Registry, Path>;
|
|
395
|
+
|
|
396
|
+
// Lookup result type (public vocabulary): the augmented constructor plus a
|
|
397
|
+
// lookup method scoped to the constructor's own type path. Non-conditional
|
|
398
|
+
// on purpose: a conditional alias resolves during inference and its
|
|
399
|
+
// expansion (internal helpers) would leak into consumer declaration emit.
|
|
400
|
+
// CallConstructs mirrors the SubTypeProxy distinction (true drops the this
|
|
401
|
+
// demand for instance-carried child constructors).
|
|
402
|
+
export type LookedUpConstructor<
|
|
403
|
+
Registry extends object,
|
|
404
|
+
Path extends keyof Registry & string,
|
|
405
|
+
CallConstructs extends boolean = false
|
|
406
|
+
> = AugmentedConstructor<Registry, Path, CallConstructs> & {
|
|
407
|
+
lookup: NestedTypeLookup<Registry, Path>;
|
|
408
|
+
};
|
|
409
|
+
|
|
374
410
|
// Result of a typed lookup: the augmented constructor plus a `lookup` method
|
|
375
411
|
// scoped to the constructor's own type path for relative subtype lookups.
|
|
376
412
|
export type LookupResult<
|
|
377
413
|
Registry extends object,
|
|
378
414
|
Path extends keyof Registry & string
|
|
379
415
|
> = Registry[Path] extends AnyConstructor
|
|
380
|
-
?
|
|
381
|
-
lookup: NestedTypeLookup<Registry, Path>;
|
|
382
|
-
}
|
|
416
|
+
? LookedUpConstructor<Registry, Path>
|
|
383
417
|
: never;
|
|
384
418
|
|
|
385
419
|
export interface TypeLookup<T extends object = GlobalRegistry> extends CallableFunction {
|
|
386
|
-
<const K extends keyof T & string>(this: unknown, TypeNestedPath: K):
|
|
420
|
+
<const K extends keyof T & string>(this: unknown, TypeNestedPath: K): LookedUpConstructor<T, K>;
|
|
387
421
|
(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
388
422
|
}
|
|
389
423
|
|
|
@@ -427,13 +461,13 @@ export type NestedTypeLookup<
|
|
|
427
461
|
Path extends string = ''
|
|
428
462
|
> = (Path extends ''
|
|
429
463
|
? {
|
|
430
|
-
<const K extends keyof Registry & string>(this: unknown, TypeNestedPath: K):
|
|
464
|
+
<const K extends keyof Registry & string>(this: unknown, TypeNestedPath: K): LookedUpConstructor<Registry, K>;
|
|
431
465
|
}
|
|
432
466
|
: {
|
|
433
467
|
<const K extends RelativeKeys<Registry, Path> & string>(
|
|
434
468
|
this: unknown,
|
|
435
469
|
TypeNestedPath: K
|
|
436
|
-
):
|
|
470
|
+
): LookedUpConstructor<Registry, Extract<keyof Registry & string, `${Path}.${K}`>>;
|
|
437
471
|
}) & {
|
|
438
472
|
(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
439
473
|
};
|
|
@@ -589,14 +623,16 @@ export type InstanceResult<
|
|
|
589
623
|
N extends object,
|
|
590
624
|
> = { [K in keyof N]: N[K] };
|
|
591
625
|
|
|
592
|
-
//
|
|
626
|
+
// The registry VALUE as public vocabulary: what a builder chain stores per
|
|
627
|
+
// defined path and what typed lookups read back. Interface (not alias) so
|
|
628
|
+
// consumer declaration emit names it instead of expanding the intersection.
|
|
593
629
|
// It preserves the dotted `Path` so that `.define()` on a looked-up constructor
|
|
594
630
|
// computes the correct child path, but it omits the full `GlobalRegistry`
|
|
595
631
|
// generic to keep hover tooltips readable.
|
|
596
|
-
export
|
|
632
|
+
export interface RegistryEntry<
|
|
597
633
|
F extends object,
|
|
598
634
|
Path extends string = ''
|
|
599
|
-
>
|
|
635
|
+
> extends _Internal_TC_<F>, RegistryHolderBase<{}, F, Path> {}
|
|
600
636
|
|
|
601
637
|
// Registry holder base - provides the accumulating .define() method.
|
|
602
638
|
// `Parent` tracks the instance type of the last defined constructor, so chained
|
|
@@ -630,7 +666,7 @@ export interface RegistryHolderBase<
|
|
|
630
666
|
): IDefinitorInstance<
|
|
631
667
|
F,
|
|
632
668
|
InstanceResult<F>,
|
|
633
|
-
T & Record<ChildPath,
|
|
669
|
+
T & Record<ChildPath, RegistryEntry<F, ChildPath>>,
|
|
634
670
|
ChildPath
|
|
635
671
|
>;
|
|
636
672
|
|
|
@@ -655,7 +691,7 @@ export interface RegistryHolderBase<
|
|
|
655
691
|
): IDefinitorInstance<
|
|
656
692
|
F,
|
|
657
693
|
InstanceResult<F>,
|
|
658
|
-
T & Record<ChildPath,
|
|
694
|
+
T & Record<ChildPath, RegistryEntry<F, ChildPath>>,
|
|
659
695
|
ChildPath
|
|
660
696
|
>;
|
|
661
697
|
}
|
|
@@ -788,7 +824,7 @@ export interface TypesCollection<
|
|
|
788
824
|
config?: constructorOptions
|
|
789
825
|
): <U extends Constructor<object>>(cstr: U) => DecoratedClass<
|
|
790
826
|
U,
|
|
791
|
-
T & Record<ConstructorName<U>,
|
|
827
|
+
T & Record<ConstructorName<U>, RegistryEntry<InstanceType<U>, ConstructorName<U>>>
|
|
792
828
|
>;
|
|
793
829
|
|
|
794
830
|
[key: string]: unknown;
|