mnemonica 1.4.7 → 1.4.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.
- package/FOR_HUMANS.md +21 -20
- package/README.md +9 -8
- package/build/api/types/compileNewModificatorFunctionBody.d.ts +4 -2
- package/build/api/types/compileNewModificatorFunctionBody.js +16 -7
- package/build/api/types/index.js +3 -3
- package/build/api/utils/index.d.ts +1 -2
- package/build/api/utils/index.js +37 -13
- package/build/index.d.ts +4 -10
- package/build/index.js +11 -29
- package/build/types/index.d.ts +205 -210
- package/build/types/index.js +1 -1
- package/docs/UTILS.md +3 -5
- package/docs/async-constructors.md +14 -11
- package/docs/hott-correspondence.md +1 -1
- package/docs/typed-lookup.md +27 -33
- package/package.json +5 -4
- package/src/api/types/compileNewModificatorFunctionBody.ts +79 -18
- package/src/api/types/index.ts +24 -25
- package/src/api/utils/index.ts +5 -51
- package/src/index.ts +40 -135
- package/src/types/index.ts +503 -490
package/src/types/index.ts
CHANGED
|
@@ -4,11 +4,10 @@
|
|
|
4
4
|
import type { TypeRegistry } from '../index';
|
|
5
5
|
import type { constants } from '../constants';
|
|
6
6
|
|
|
7
|
-
// Props type for getProps/setProps
|
|
8
|
-
export type PropsType = Record<string, unknown>;
|
|
9
|
-
|
|
10
7
|
// Core type definitions for mnemonica
|
|
11
8
|
|
|
9
|
+
// --- handler and callable shapes -------------------------------------------
|
|
10
|
+
|
|
12
11
|
// Base constructor function type - can be a class constructor or a function.
|
|
13
12
|
// Args defaults to unknown[] so IDEF<T> is backwards compatible;
|
|
14
13
|
// supply Args to get typed constructor params: IDEF<MyType, [string, number]>
|
|
@@ -55,41 +54,7 @@ export type DefineNewableOrCallable = DefineNewable | DefineCallable;
|
|
|
55
54
|
// that reference must stay assignable here.
|
|
56
55
|
export interface InstanceConstructor extends NewableFunction {}
|
|
57
56
|
|
|
58
|
-
//
|
|
59
|
-
// (HookableConstructor's registerHook, the WithHookRegister member check):
|
|
60
|
-
// it consumes whatever opts the caller's typed hook carries and returns
|
|
61
|
-
// unknown. never[] parameters keep every hook shape assignable by
|
|
62
|
-
// contravariance, including generic typedHook<HT> instantiations (a
|
|
63
|
-
// parameter typed as the plain `hook` interface would reject those).
|
|
64
|
-
export interface AnyHookCallback extends CallableFunction {
|
|
65
|
-
(...args: never[]): unknown;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// A utility method as exposed on the `utils` collection — the
|
|
69
|
-
// index-signature fallback for utilities beyond the declared set. Plain
|
|
70
|
-
// callables: any argument list, unknown result.
|
|
71
|
-
export interface UtilFunction extends CallableFunction {
|
|
72
|
-
(...args: never[]): unknown;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// The minimal source shape the free lookup() resolves against: any value
|
|
76
|
-
// carrying a lookup method with the untyped fallback signature
|
|
77
|
-
// (a TypesCollection, a type constructor, or the mnemonica module object).
|
|
78
|
-
export interface LookupSource {
|
|
79
|
-
lookup(path: string): TypeClass | undefined;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// The modification-error sink handed through the creator object: consumes
|
|
83
|
-
// the construction error, produces nothing (the construction aborts).
|
|
84
|
-
export interface ThrowModificationError extends CallableFunction {
|
|
85
|
-
(error: Error): void;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// The bound fork invoker that instance.fork() returns: consumes fork
|
|
89
|
-
// arguments, produces a fresh instance of the forked type.
|
|
90
|
-
export interface ForkInvoker<T extends object> extends CallableFunction {
|
|
91
|
-
(this: object | void, ...forkArgs: unknown[]): T;
|
|
92
|
-
}
|
|
57
|
+
// --- errors ----------------------------------------------------------------
|
|
93
58
|
|
|
94
59
|
// Error message types - all error messages are strings
|
|
95
60
|
export type ErrorMessageKey =
|
|
@@ -111,21 +76,6 @@ export type ErrorMessageKey =
|
|
|
111
76
|
// Error messages object type
|
|
112
77
|
export type ErrorMessages = Record<ErrorMessageKey, string>;
|
|
113
78
|
|
|
114
|
-
// Hook callback as STORED and invoked by invokeHook — its parameter is the
|
|
115
|
-
// DELIVERED payload (deliveredHookOpts: HookInvocation.build()'s output).
|
|
116
|
-
// The registration surface (registerHook) types callbacks per hookType via
|
|
117
|
-
// typedHook instead.
|
|
118
|
-
export interface hook extends CallableFunction {
|
|
119
|
-
(opts: deliveredHookOpts): unknown;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// Flow checker callback registered via registerFlowChecker: invoked with
|
|
123
|
-
// the hook invocation record after each hook run; its return value is
|
|
124
|
-
// collected into the invocation results but otherwise unused.
|
|
125
|
-
export interface FlowChecker extends CallableFunction {
|
|
126
|
-
(opts: object): unknown;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
79
|
// An error class made by constructError(name, message) — the lib.es5
|
|
130
80
|
// ErrorConstructor idiom, `new`-only (a class cannot be called without it).
|
|
131
81
|
// Each class carries one fixed message (constants ErrorMessages); `addition`
|
|
@@ -136,9 +86,6 @@ export interface MnemonicaErrorConstructor extends NewableFunction {
|
|
|
136
86
|
readonly prototype: MnemonicaError;
|
|
137
87
|
}
|
|
138
88
|
|
|
139
|
-
// Errors types map - indexable record of error constructors
|
|
140
|
-
export type ErrorsTypesMap = Record<string, MnemonicaErrorConstructor>;
|
|
141
|
-
|
|
142
89
|
// Mnemonica-processed error. It carries no custom properties of its own:
|
|
143
90
|
// the error data lives in the external props storage (WeakMap) — read it
|
|
144
91
|
// via getProps(error), typed as ErrorProps below.
|
|
@@ -179,6 +126,8 @@ export interface ErrorProps {
|
|
|
179
126
|
*/
|
|
180
127
|
export type CreationError<T extends object> = ( T & Error ) | Error;
|
|
181
128
|
|
|
129
|
+
// --- constructor shapes ----------------------------------------------------
|
|
130
|
+
|
|
182
131
|
/**
|
|
183
132
|
* Internal Type Constructor.
|
|
184
133
|
* "Type" here is used in the Computer Science sense — an interface/contract
|
|
@@ -225,68 +174,48 @@ export interface TypeConstructorBase {
|
|
|
225
174
|
*/
|
|
226
175
|
export type AnyConstructor = new (...args: never[]) => unknown;
|
|
227
176
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
177
|
+
// Constructor type for decorate function.
|
|
178
|
+
// Parameters are `never[]` for the same reason as AnyConstructor: under
|
|
179
|
+
// strictFunctionTypes an `unknown[]` constraint rejects classes with precise
|
|
180
|
+
// constructor parameters, so `@decorate()` could not be applied to them.
|
|
181
|
+
export type Constructor<T = object> = new (...args: never[]) => T;
|
|
234
182
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
* without picking up the `[key: string]` signature.
|
|
239
|
-
*/
|
|
240
|
-
export type LiteralKeysOf<T> = keyof T extends infer K
|
|
241
|
-
? K extends string
|
|
242
|
-
? string extends K ? never : K
|
|
243
|
-
: never
|
|
244
|
-
: never;
|
|
183
|
+
// Name of a constructor as a string literal type.
|
|
184
|
+
export type ConstructorName<T extends Constructor<object>> =
|
|
185
|
+
T extends { name: infer N } ? N extends string ? N : string : string;
|
|
245
186
|
|
|
246
|
-
|
|
247
|
-
* Given a dotted TypeRegistry key, extract the parent path.
|
|
248
|
-
* `ParentPath<'A.B.C'>` → `'A.B'`.
|
|
249
|
-
*/
|
|
250
|
-
export type ParentPath<K extends string> =
|
|
251
|
-
K extends `${infer P}.${string}` ? P : never;
|
|
187
|
+
// --- hooks -----------------------------------------------------------------
|
|
252
188
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
189
|
+
// A hook callback as seen by a REGISTERING surface that only forwards it
|
|
190
|
+
// (HookableConstructor's registerHook, the WithHookRegister member check):
|
|
191
|
+
// it consumes whatever opts the caller's typed hook carries and returns
|
|
192
|
+
// unknown. never[] parameters keep every hook shape assignable by
|
|
193
|
+
// contravariance, including generic typedHook<HT> instantiations (a
|
|
194
|
+
// parameter typed as the plain `hook` interface would reject those).
|
|
195
|
+
export interface AnyHookCallback extends CallableFunction {
|
|
196
|
+
(...args: never[]): unknown;
|
|
197
|
+
}
|
|
259
198
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
[K in keyof TypeRegistry]: K extends `${P}.${string}` ? K : never
|
|
266
|
-
}[keyof TypeRegistry];
|
|
199
|
+
// The modification-error sink handed through the creator object: consumes
|
|
200
|
+
// the construction error, produces nothing (the construction aborts).
|
|
201
|
+
export interface ThrowModificationError extends CallableFunction {
|
|
202
|
+
(error: Error): void;
|
|
203
|
+
}
|
|
267
204
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
[K in LiteralKeysOf<TypeRegistry>]: TypeRegistry[K] extends new (...args: never[]) => infer R
|
|
276
|
-
? T extends R ? K : never
|
|
277
|
-
: never
|
|
278
|
-
}[LiteralKeysOf<TypeRegistry>];
|
|
205
|
+
// Hook callback as STORED and invoked by invokeHook — its parameter is the
|
|
206
|
+
// DELIVERED payload (deliveredHookOpts: HookInvocation.build()'s output).
|
|
207
|
+
// The registration surface (registerHook) types callbacks per hookType via
|
|
208
|
+
// typedHook instead.
|
|
209
|
+
export interface hook extends CallableFunction {
|
|
210
|
+
(opts: deliveredHookOpts): unknown;
|
|
211
|
+
}
|
|
279
212
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
[K in LiteralKeysOf<TypeRegistry>]: TypeRegistry[K] extends new (...args: never[]) => infer R
|
|
287
|
-
? T extends R ? AllParentPrefixes<K> : never
|
|
288
|
-
: never
|
|
289
|
-
}[LiteralKeysOf<TypeRegistry>];
|
|
213
|
+
// Flow checker callback registered via registerFlowChecker: invoked with
|
|
214
|
+
// the hook invocation record after each hook run; its return value is
|
|
215
|
+
// collected into the invocation results but otherwise unused.
|
|
216
|
+
export interface FlowChecker extends CallableFunction {
|
|
217
|
+
(opts: object): unknown;
|
|
218
|
+
}
|
|
290
219
|
|
|
291
220
|
// Hook types
|
|
292
221
|
export type hooksTypes = 'preCreation' | 'postCreation' | 'creationError';
|
|
@@ -377,6 +306,21 @@ export type HookableConstructor<T extends object> = Constructor<T> & {
|
|
|
377
306
|
registerHook(hookType: hooksTypes, cb: AnyHookCallback): void;
|
|
378
307
|
};
|
|
379
308
|
|
|
309
|
+
// Shared interface for objects that support hooks (TypeDef and CollectionDef)
|
|
310
|
+
export interface Hookable {
|
|
311
|
+
hooks: Record<string, Set<hook>>;
|
|
312
|
+
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
|
+
registerFlowChecker(cb: FlowChecker): void;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// --- config and runtime internals ------------------------------------------
|
|
323
|
+
|
|
380
324
|
// Callback passed into ModificationConstructor to attach internal props to the prototype
|
|
381
325
|
export interface AddPropsCallback extends CallableFunction {
|
|
382
326
|
(proto: object): void;
|
|
@@ -463,7 +407,6 @@ export interface CollectionLookup extends CallableFunction {
|
|
|
463
407
|
(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
464
408
|
}
|
|
465
409
|
|
|
466
|
-
// Collection definition
|
|
467
410
|
// A collection's config, stored under SymbolConfig. Every collection carries
|
|
468
411
|
// a name by construction: 'defaultTypes' for the default one, the given or
|
|
469
412
|
// auto-numbered name for custom ones (createTypesCollection).
|
|
@@ -472,6 +415,7 @@ export type CollectionConfig = {
|
|
|
472
415
|
[key: string]: unknown;
|
|
473
416
|
};
|
|
474
417
|
|
|
418
|
+
// Collection definition
|
|
475
419
|
// the config key is SymbolConfig's own unique symbol type; a mapped type
|
|
476
420
|
// over it keeps this module type-only (no runtime import of constants)
|
|
477
421
|
export type CollectionDef = Hookable & {
|
|
@@ -483,79 +427,236 @@ export type CollectionDef = Hookable & {
|
|
|
483
427
|
[K in typeof constants.SymbolConfig]: CollectionConfig;
|
|
484
428
|
};
|
|
485
429
|
|
|
486
|
-
//
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
// define() results without a TypeRegistry merge — see docs/typed-lookup.md,
|
|
493
|
-
// "Declaration emit on TypeScript 6". Do not re-export this.
|
|
494
|
-
export type GlobalRegistry = TypeRegistry & Record<string, TypeConstructorBase>;
|
|
430
|
+
// Specification for chained subtype creation with .then()
|
|
431
|
+
export interface ThenSpec {
|
|
432
|
+
subtype: object;
|
|
433
|
+
args: unknown[];
|
|
434
|
+
name?: string;
|
|
435
|
+
}
|
|
495
436
|
|
|
496
|
-
//
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
437
|
+
// Context object passed through the InstanceCreator pipeline
|
|
438
|
+
export interface InstanceCreatorContext {
|
|
439
|
+
type: TypeDef;
|
|
440
|
+
TypeName: string;
|
|
441
|
+
existentInstance: object;
|
|
442
|
+
args: unknown[];
|
|
443
|
+
ModificationConstructor: ModificationConstructor;
|
|
444
|
+
ModificatorType: MnemonicaConstructor;
|
|
445
|
+
InstanceModificator: MnemonicaConstructor;
|
|
446
|
+
inheritedInstance: object | Promise<object>;
|
|
447
|
+
config: constructorOptions;
|
|
448
|
+
proto: object;
|
|
449
|
+
__proto_proto__?: object;
|
|
450
|
+
stack?: string[];
|
|
451
|
+
// a lazy type's gather outcome, snapshotted by runSetup before the cell
|
|
452
|
+
// is reset (see lazyDefinition.ts): failed is the explicit flag (a
|
|
453
|
+
// callback may throw undefined), lazyError the thrown value itself —
|
|
454
|
+
// passed through to throwModificationError exactly like a
|
|
455
|
+
// constructor-body throw
|
|
456
|
+
lazyFailed?: boolean;
|
|
457
|
+
lazyError?: unknown;
|
|
458
|
+
// set by throwModificationError once the post hooks have fired for this
|
|
459
|
+
// construction; postProcessing reads it to avoid a second firing when a
|
|
460
|
+
// creationError hook suppressed the throw
|
|
461
|
+
postHooksFired?: boolean;
|
|
505
462
|
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
463
|
+
getExistentAsyncStack(existentInstance: object): unknown;
|
|
464
|
+
postProcessing(continuationOf?: TypeDef): void;
|
|
465
|
+
makeAwaiter(type: TypeDef, then?: ThenSpec): Promise<object>;
|
|
466
|
+
addThen(then: ThenSpec): void;
|
|
467
|
+
invokePreHooks(): void;
|
|
468
|
+
invokePostHooks(): { type: Set<unknown>; collection: Set<unknown> };
|
|
469
|
+
throwModificationError(error: MnemonicaError): void;
|
|
470
|
+
}
|
|
512
471
|
|
|
513
|
-
//
|
|
514
|
-
//
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
472
|
+
// Internal instance properties (non-enumerable)
|
|
473
|
+
// These are always present on instances but accessed via getProps/setProps
|
|
474
|
+
export type InstanceInternalProps = {
|
|
475
|
+
__proto_proto__: object;
|
|
476
|
+
__args__: unknown[];
|
|
477
|
+
__collection__: CollectionDef;
|
|
478
|
+
__subtypes__: Map<string, object>;
|
|
479
|
+
__type__: TypeDef;
|
|
480
|
+
__parent__: object;
|
|
481
|
+
__stack__?: string;
|
|
482
|
+
__creator__: InstanceCreatorContext;
|
|
483
|
+
__timestamp__: number;
|
|
523
484
|
};
|
|
524
485
|
|
|
525
|
-
//
|
|
526
|
-
//
|
|
527
|
-
//
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
new (...args: infer A): unknown;
|
|
486
|
+
// Combined Props type for internal use inside ./src
|
|
487
|
+
// Instance properties for __self__ reference
|
|
488
|
+
// This merges internal props with the __self__ reference and an open index
|
|
489
|
+
export type Props =InstanceInternalProps & {
|
|
490
|
+
__self__: InstanceInternalProps;
|
|
491
|
+
[key: string]: unknown;
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
// Mnemonica constructor — the constructor function returned by constructHandler()
|
|
495
|
+
// Has SymbolConstructorName attached and is both newable and callable
|
|
496
|
+
export interface MnemonicaConstructor extends NewableFunction {
|
|
497
|
+
new (...args: unknown[]): object;
|
|
498
|
+
[key: symbol]: unknown;
|
|
539
499
|
}
|
|
540
|
-
|
|
500
|
+
|
|
501
|
+
// Type descriptor instance — internal shape of TypeDescriptor objects
|
|
502
|
+
export interface TypeDescriptorDefine extends CallableFunction {
|
|
503
|
+
(
|
|
504
|
+
TypeOrTypeName: string | DefineNewableOrCallable,
|
|
505
|
+
constructHandlerOrConfig?: DefineNewableOrCallable | object,
|
|
506
|
+
config?: object
|
|
507
|
+
): TypeClass;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
export interface TypeDescriptorLazy extends CallableFunction {
|
|
511
|
+
(
|
|
512
|
+
TypeName: string,
|
|
513
|
+
getter: LazyDef<object>,
|
|
514
|
+
config?: object
|
|
515
|
+
): TypeClass;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
export interface TypeDescriptorLookup extends CallableFunction {
|
|
519
|
+
(TypeNestedPath: string): TypeClass | undefined;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
export type TypeDescriptorInstance = {
|
|
523
|
+
define: TypeDescriptorDefine;
|
|
524
|
+
lazy: TypeDescriptorLazy;
|
|
525
|
+
decorate: (config?: constructorOptions) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U>;
|
|
526
|
+
lookup: TypeDescriptorLookup;
|
|
527
|
+
subtypes: Map<string, object>;
|
|
528
|
+
TypeName: string;
|
|
529
|
+
collection: CollectionDef;
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
// --- registry and lookup ---------------------------------------------------
|
|
533
|
+
|
|
534
|
+
// The minimal source shape the free lookup() resolves against: any value
|
|
535
|
+
// carrying a lookup method with the untyped fallback signature
|
|
536
|
+
// (a TypesCollection, a type constructor, or the mnemonica module object).
|
|
537
|
+
export interface LookupSource {
|
|
538
|
+
lookup(path: string): TypeClass | undefined;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Instance type produced by a TypeRegistry constructor.
|
|
543
|
+
*/
|
|
544
|
+
export type InstanceOfTypeRegistry<K extends keyof TypeRegistry> =
|
|
545
|
+
TypeRegistry[K] extends new (...args: never[]) => infer R ? R : never;
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Extract only the literal string keys of a type, filtering out generic
|
|
549
|
+
* index signatures. This lets us iterate over augmented TypeRegistry keys
|
|
550
|
+
* without picking up the `[key: string]` signature.
|
|
551
|
+
*/
|
|
552
|
+
export type LiteralKeysOf<T> = keyof T extends infer K
|
|
553
|
+
? K extends string
|
|
554
|
+
? string extends K ? never : K
|
|
555
|
+
: never
|
|
556
|
+
: never;
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* Given a dotted TypeRegistry key, return the union of all ancestor prefixes.
|
|
560
|
+
* `AllParentPrefixes<'A.B.C'>` → `'A' | 'A.B'`.
|
|
561
|
+
*/
|
|
562
|
+
export type AllParentPrefixes<K extends string> =
|
|
563
|
+
K extends `${infer P}.${string}` ? P | AllParentPrefixes<P> : never;
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Given an instance type, return the union of TypeRegistry paths that are
|
|
567
|
+
* valid parent lookups for that instance: the registry is inverted at the
|
|
568
|
+
* type level (the key(s) whose constructor returns that instance type), so
|
|
569
|
+
* utilities like `parent()` derive an instance's parent paths from its type
|
|
570
|
+
* alone. Root types produce `never` because they have no parent path.
|
|
571
|
+
*/
|
|
572
|
+
export type ParentPathOfInstance<T extends object> = {
|
|
573
|
+
[K in LiteralKeysOf<TypeRegistry>]: TypeRegistry[K] extends new (...args: never[]) => infer R
|
|
574
|
+
? T extends R ? AllParentPrefixes<K> : never
|
|
575
|
+
: never
|
|
576
|
+
}[LiteralKeysOf<TypeRegistry>];
|
|
577
|
+
|
|
578
|
+
// Global registry shape used as the default for TypeLookup. It combines any
|
|
579
|
+
// augmented TypeRegistry entries with a broad string index signature so the
|
|
580
|
+
// generic TypeLookup constraint is satisfied.
|
|
581
|
+
// Internal on purpose: if you are here from a TS2883 error ("cannot be
|
|
582
|
+
// named without a reference to 'GlobalRegistry'"), the consumer exports free
|
|
583
|
+
// define() results without a TypeRegistry merge — see docs/typed-lookup.md,
|
|
584
|
+
// "Declaration emit on TypeScript 6". Do not re-export this.
|
|
585
|
+
export type GlobalRegistry = TypeRegistry & Record<string, TypeConstructorBase>;
|
|
586
|
+
|
|
587
|
+
// Extract the instance type returned by a mnemonica-compatible constructor.
|
|
588
|
+
// Async entries construct a Promise, but inheritance, hooks, and subtype
|
|
589
|
+
// lookup all operate on the resolved instance. Falls back to `object` so
|
|
590
|
+
// generic helpers that require an object constraint do not fail when the
|
|
591
|
+
// constructor shape is not yet narrowed.
|
|
592
|
+
export type ExtractConstructorInstance<C> =
|
|
593
|
+
C extends { new (...args: never[]): infer I }
|
|
594
|
+
? Awaited<I> extends object ? Awaited<I> : object
|
|
595
|
+
: object;
|
|
596
|
+
|
|
597
|
+
// Preserve the entry's construction kind when replacing its instance shape:
|
|
598
|
+
// an async entry stays Promise-returning on both new and call signatures.
|
|
599
|
+
export type ConstructResult<C, NewInstance extends object> =
|
|
600
|
+
C extends { new (...args: never[]): PromiseLike<unknown> }
|
|
601
|
+
? Promise<NewInstance>
|
|
602
|
+
: NewInstance;
|
|
603
|
+
|
|
604
|
+
// Map dotted registry paths that extend `${Path}.${Child}` into child
|
|
605
|
+
// constructor properties on the parent instance. This lets typed lookups
|
|
606
|
+
// return a constructor whose instances carry their subtypes, e.g.
|
|
607
|
+
// `new (App.lookup('User'))(...).Admin`.
|
|
608
|
+
export type SubTypeConstructors<
|
|
609
|
+
Registry extends object,
|
|
610
|
+
Path extends string
|
|
611
|
+
> = {
|
|
612
|
+
[K in keyof Registry as K extends `${Path}.${infer Child}` ? Child : never]:
|
|
613
|
+
LookedUpConstructor<Registry, K & string, true>;
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
// Reconstruct a constructor with a new instance type while preserving every
|
|
617
|
+
// non-`prototype`, non-`lookup` property (`define`, hooks, etc.). `lookup` is
|
|
618
|
+
// replaced separately so it can be scoped to the constructor's own type path.
|
|
619
|
+
// The match requires only the construct signature: tactica-emitted and
|
|
620
|
+
// hand-written registry entries are bare newables (no call signature, no
|
|
621
|
+
// declared `prototype`), and demanding those members collapses the lookup
|
|
622
|
+
// result to `never` for precisely-typed registries. A call signature is
|
|
623
|
+
// re-added only when the source constructor carries one
|
|
624
|
+
// (ReplacedCallSignature).
|
|
625
|
+
export type ReplaceConstructorInstance<
|
|
626
|
+
C,
|
|
627
|
+
NewInstance extends object,
|
|
628
|
+
CallConstructs extends boolean = false
|
|
629
|
+
> = C extends {
|
|
630
|
+
new (...args: infer A): unknown;
|
|
631
|
+
}
|
|
632
|
+
? {
|
|
541
633
|
new (...args: A): ConstructResult<C, NewInstance>;
|
|
542
634
|
readonly prototype: NewInstance & {
|
|
543
635
|
readonly constructor: ReplaceConstructorInstance<C, NewInstance, CallConstructs>;
|
|
544
636
|
};
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
// SubTypeProxy-backed member: the call form constructs the child
|
|
548
|
-
// from the parent instance (the chain-tip form new R().A()), so
|
|
549
|
-
// no `this` demand — detached bare calls construct too.
|
|
550
|
-
? { (...args: A2): ConstructResult<C, NewInstance> }
|
|
551
|
-
// Raw looked-up constructor: the `this` guard honestly models
|
|
552
|
-
// .call(childInstance, args); a bare call here is the decorator
|
|
553
|
-
// path at runtime, not construction.
|
|
554
|
-
: { (this: NewInstance, ...args: A2): ConstructResult<C, NewInstance> }
|
|
555
|
-
: unknown)
|
|
637
|
+
}
|
|
638
|
+
& ReplacedCallSignature<C, NewInstance, CallConstructs>
|
|
556
639
|
& Omit<C, 'prototype' | 'lookup'>
|
|
557
640
|
: never;
|
|
558
641
|
|
|
642
|
+
// The call signature ReplaceConstructorInstance keeps: none (unknown) when
|
|
643
|
+
// the source constructor has no call signature, otherwise one of two forms.
|
|
644
|
+
export type ReplacedCallSignature<
|
|
645
|
+
C,
|
|
646
|
+
NewInstance extends object,
|
|
647
|
+
CallConstructs extends boolean
|
|
648
|
+
> = C extends { (...args: infer A): unknown }
|
|
649
|
+
? CallConstructs extends true
|
|
650
|
+
// SubTypeProxy-backed member: the call form constructs the child
|
|
651
|
+
// from the parent instance (the chain-tip form new R().A()), so
|
|
652
|
+
// no `this` demand — detached bare calls construct too.
|
|
653
|
+
? { (...args: A): ConstructResult<C, NewInstance> }
|
|
654
|
+
// Raw looked-up constructor: the `this` guard honestly models
|
|
655
|
+
// .call(childInstance, args); a bare call here is the decorator
|
|
656
|
+
// path at runtime, not construction.
|
|
657
|
+
: { (this: NewInstance, ...args: A): ConstructResult<C, NewInstance> }
|
|
658
|
+
: unknown;
|
|
659
|
+
|
|
559
660
|
// Instance type produced by a typed lookup: the constructor's own instance
|
|
560
661
|
// shape plus child-constructor properties for every registered subtype.
|
|
561
662
|
// Kept as a named alias so hover tooltips show the base shape first instead of
|
|
@@ -589,17 +690,37 @@ export type LookedUpInstance<
|
|
|
589
690
|
Path extends string
|
|
590
691
|
> = WithSubTypes<ExtractConstructorInstance<Registry[Path & keyof Registry]>, Registry, Path>;
|
|
591
692
|
|
|
693
|
+
// Type-only key: no runtime property carries it. It holds a looked-up
|
|
694
|
+
// constructor's own type path (see TypePathMark below).
|
|
695
|
+
declare const TypePathKey: unique symbol;
|
|
696
|
+
|
|
697
|
+
// A looked-up constructor's type path as a member TypeScript compares first.
|
|
698
|
+
// Runtime types are nominal: constructors of different paths are different
|
|
699
|
+
// types even when their shapes match. Without this member, relating two
|
|
700
|
+
// looked-up constructors of different paths walks both subtrees member by
|
|
701
|
+
// member (their path's variance is unreliable, through the template-literal
|
|
702
|
+
// key remapping), at a cost exponential in the subtree's depth and fan, and
|
|
703
|
+
// the checker runs out of memory, e.g. when an instance is passed to a
|
|
704
|
+
// parameter typed as a union of generated types with the wrong member
|
|
705
|
+
// first. With it, such a relation fails at this member.
|
|
706
|
+
export type TypePathMark<Path extends string> = {
|
|
707
|
+
readonly [TypePathKey]?: Path;
|
|
708
|
+
};
|
|
709
|
+
|
|
592
710
|
// Lookup result type (public vocabulary): the full-registry definition
|
|
593
711
|
// surface plus the augmented constructor and a lookup method scoped to the
|
|
594
712
|
// constructor's own type path. Non-conditional on purpose: a conditional
|
|
595
713
|
// alias resolves during inference and its expansion (internal helpers) would
|
|
596
714
|
// leak into consumer declaration emit. CallConstructs mirrors the SubTypeProxy
|
|
597
715
|
// distinction (true drops the this demand for instance-carried child constructors).
|
|
716
|
+
// TypePathMark comes first: an intersection target is checked member by
|
|
717
|
+
// member in order, so a different path is rejected before anything else.
|
|
598
718
|
export type LookedUpConstructor<
|
|
599
719
|
Registry extends object,
|
|
600
720
|
Path extends keyof Registry & string,
|
|
601
721
|
CallConstructs extends boolean = false
|
|
602
|
-
> =
|
|
722
|
+
> = TypePathMark<Path> &
|
|
723
|
+
RegistryHolderBase<Registry, ExtractConstructorInstance<Registry[Path]>, Path> &
|
|
603
724
|
AugmentedConstructor<Registry, Path, CallConstructs> & {
|
|
604
725
|
lookup: NestedTypeLookup<Registry, Path>;
|
|
605
726
|
};
|
|
@@ -640,6 +761,7 @@ export type LookupResult<
|
|
|
640
761
|
? LookedUpConstructor<Registry, Path>
|
|
641
762
|
: never;
|
|
642
763
|
|
|
764
|
+
// Type lookup function type: may have augmentation by Tactica re-definition
|
|
643
765
|
export interface TypeLookup<T extends object = GlobalRegistry> extends CallableFunction {
|
|
644
766
|
<const K extends keyof T & string>(this: unknown, TypeNestedPath: K): LookedUpConstructor<T, K>;
|
|
645
767
|
(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
@@ -678,13 +800,6 @@ export type RelativeKeys<
|
|
|
678
800
|
? K extends `${Path}.${infer Child}` ? Child : never
|
|
679
801
|
: never);
|
|
680
802
|
|
|
681
|
-
// Full registry key that corresponds to a relative key `K` under `Path`.
|
|
682
|
-
export type FullKey<
|
|
683
|
-
Registry extends object,
|
|
684
|
-
Path extends string,
|
|
685
|
-
K extends string
|
|
686
|
-
> = Path extends '' ? K : Extract<keyof Registry & string, `${Path}.${K}`>;
|
|
687
|
-
|
|
688
803
|
// Lookup function type for constructors and registry holders that know their
|
|
689
804
|
// own dotted type path. Root holders use `Path = ''` and behave like the global
|
|
690
805
|
// `TypeLookup`; child constructors use their own path for relative lookups.
|
|
@@ -697,243 +812,14 @@ export type NestedTypeLookup<
|
|
|
697
812
|
}
|
|
698
813
|
: {
|
|
699
814
|
<const K extends RelativeKeys<Registry, Path> & string>(
|
|
700
|
-
this: unknown,
|
|
701
|
-
TypeNestedPath: K
|
|
702
|
-
): LookedUpConstructor<Registry, Extract<keyof Registry & string, `${Path}.${K}`>>;
|
|
703
|
-
}) & {
|
|
704
|
-
(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
705
|
-
};
|
|
706
|
-
|
|
707
|
-
// Specification for chained subtype creation with .then()
|
|
708
|
-
export interface ThenSpec {
|
|
709
|
-
subtype: object;
|
|
710
|
-
args: unknown[];
|
|
711
|
-
name?: string;
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
// Context object passed through the InstanceCreator pipeline
|
|
715
|
-
export interface InstanceCreatorContext {
|
|
716
|
-
type: TypeDef;
|
|
717
|
-
TypeName: string;
|
|
718
|
-
existentInstance: object;
|
|
719
|
-
args: unknown[];
|
|
720
|
-
ModificationConstructor: ModificationConstructor;
|
|
721
|
-
ModificatorType: MnemonicaConstructor;
|
|
722
|
-
InstanceModificator: MnemonicaConstructor;
|
|
723
|
-
inheritedInstance: object | Promise<object>;
|
|
724
|
-
config: constructorOptions;
|
|
725
|
-
proto: object;
|
|
726
|
-
__proto_proto__?: object;
|
|
727
|
-
stack?: string[];
|
|
728
|
-
// a lazy type's gather outcome, snapshotted by runSetup before the cell
|
|
729
|
-
// is reset (see lazyDefinition.ts): failed is the explicit flag (a
|
|
730
|
-
// callback may throw undefined), lazyError the thrown value itself —
|
|
731
|
-
// passed through to throwModificationError exactly like a
|
|
732
|
-
// constructor-body throw
|
|
733
|
-
lazyFailed?: boolean;
|
|
734
|
-
lazyError?: unknown;
|
|
735
|
-
// set by throwModificationError once the post hooks have fired for this
|
|
736
|
-
// construction; postProcessing reads it to avoid a second firing when a
|
|
737
|
-
// creationError hook suppressed the throw
|
|
738
|
-
postHooksFired?: boolean;
|
|
739
|
-
|
|
740
|
-
getExistentAsyncStack(existentInstance: object): unknown;
|
|
741
|
-
postProcessing(continuationOf?: TypeDef): void;
|
|
742
|
-
makeAwaiter(type: TypeDef, then?: ThenSpec): Promise<object>;
|
|
743
|
-
addThen(then: ThenSpec): void;
|
|
744
|
-
invokePreHooks(): void;
|
|
745
|
-
invokePostHooks(): { type: Set<unknown>; collection: Set<unknown> };
|
|
746
|
-
throwModificationError(error: MnemonicaError): void;
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
/**
|
|
750
|
-
* Proto merge type - combines parent and child types without property conflicts.
|
|
751
|
-
*
|
|
752
|
-
* When a subtype is defined from a parent instance, we want the child properties
|
|
753
|
-
* to take precedence. `Exclude<keyof P, keyof T>` removes overlapping keys from P,
|
|
754
|
-
* so `Pick<P, ...>` only brings in parent properties that don't clash with child.
|
|
755
|
-
*
|
|
756
|
-
* Result: child T gets all its own props, plus parent's non-overlapping props.
|
|
757
|
-
*/
|
|
758
|
-
// When the parent contributes no properties, collapse to the child type so
|
|
759
|
-
// hover tooltips stay readable.
|
|
760
|
-
export type Proto<P extends object, T extends object> =
|
|
761
|
-
[Exclude<keyof P, keyof T>] extends [never]
|
|
762
|
-
? T
|
|
763
|
-
: T & Pick<P, Exclude<keyof P, keyof T>>;
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
// export type ProtoFlat<
|
|
767
|
-
// P extends object,
|
|
768
|
-
// T extends object,
|
|
769
|
-
// F extends Proto<P, T> = Proto<P, T>
|
|
770
|
-
// > = { [key in keyof F]: F[key]} ;
|
|
771
|
-
|
|
772
|
-
export type ProtoFlat<
|
|
773
|
-
P extends object,
|
|
774
|
-
T extends object,
|
|
775
|
-
L extends Exclude<keyof P, keyof T> = Exclude<keyof P, keyof T>,
|
|
776
|
-
> = {
|
|
777
|
-
[key in keyof T]: T[key];
|
|
778
|
-
} & {
|
|
779
|
-
[key in L]: P[key];
|
|
780
|
-
};
|
|
781
|
-
|
|
782
|
-
// Mnemonica instance method keys. These are filtered out of extracted/merged
|
|
783
|
-
// field-only views because they belong to the prototype, not to the user's
|
|
784
|
-
// enumerable data properties.
|
|
785
|
-
export type MnemonicaInstanceMethodKeys =
|
|
786
|
-
'extract' | 'pick' | 'parent' | 'clone' | 'fork' | 'exception' | 'sibling';
|
|
787
|
-
|
|
788
|
-
export type Flatten<F> = { [key in keyof F]: F[key] }
|
|
789
|
-
|
|
790
|
-
// Extracted<T>: the shape returned by utils.extract(instance).
|
|
791
|
-
// It contains the enumerable string-keyed user properties of the instance,
|
|
792
|
-
// omitting MnemonicaInstance methods (extract, pick, parent, clone, fork,
|
|
793
|
-
// exception, sibling). Optionality is preserved from the source type.
|
|
794
|
-
// The `& {}` trick forces TypeScript to expand the alias in hover tooltips
|
|
795
|
-
// so it renders as a plain object literal instead of "Extracted<{...}>".
|
|
796
|
-
export type Extracted<T extends object> = {
|
|
797
|
-
[K in keyof T as K extends string ? (K extends MnemonicaInstanceMethodKeys ? never : K) : never]: T[K];
|
|
798
|
-
} & {};
|
|
799
|
-
|
|
800
|
-
// Parsed<T>: the shape returned by utils.parse(instance).
|
|
801
|
-
// It is a one-level snapshot of the instance's prototype chain.
|
|
802
|
-
// `props` contains the instance's enumerable user properties;
|
|
803
|
-
// `joint` contains enumerable properties copied from the immediate prototype;
|
|
804
|
-
// `parent` is the parent INSTANCE — the same object utils.parent(instance)
|
|
805
|
-
// returns; null for no parent. Not recursively parsed — walking the whole
|
|
806
|
-
// lineage level by level is what utils.deepParse does — this function stays
|
|
807
|
-
// the one-level primitive.
|
|
808
|
-
// The three "no parent" readings, stated once:
|
|
809
|
-
// parent == null — no parent at all (covers both below);
|
|
810
|
-
// parent === null — the end of a REAL chain (a root instance);
|
|
811
|
-
// parent === undefined — nothing was given (utils.parse(null)).
|
|
812
|
-
export type Parsed<T extends object> = {
|
|
813
|
-
name: string;
|
|
814
|
-
props: Extracted<T>;
|
|
815
|
-
self: T;
|
|
816
|
-
proto: object;
|
|
817
|
-
joint: Record<string, unknown>;
|
|
818
|
-
parent: object | null;
|
|
819
|
-
};
|
|
820
|
-
|
|
821
|
-
// EmptyParsed: the shape utils.parse(null) returns — the one non-object
|
|
822
|
-
// input parse accepts. "Nothing was given": every field is its empty value,
|
|
823
|
-
// and parent is undefined (see the readings above).
|
|
824
|
-
export interface EmptyParsed {
|
|
825
|
-
name: undefined;
|
|
826
|
-
props: {};
|
|
827
|
-
self: null;
|
|
828
|
-
proto: undefined;
|
|
829
|
-
joint: {};
|
|
830
|
-
parent: undefined;
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
// --- lineage (the lethe cross-language export) -----------------------------
|
|
834
|
-
|
|
835
|
-
// A JSON-safe value in the lethe lineage graph: instances export as $ref,
|
|
836
|
-
// non-JSON values as tagged placeholders — never an error.
|
|
837
|
-
export type LineageValue =
|
|
838
|
-
| null
|
|
839
|
-
| boolean
|
|
840
|
-
| string
|
|
841
|
-
| number
|
|
842
|
-
| LineageValue[]
|
|
843
|
-
| { $ref: string }
|
|
844
|
-
| { '$mnemonica': 'unsupported'; kind: string }
|
|
845
|
-
| { [ key: string ]: LineageValue };
|
|
846
|
-
|
|
847
|
-
// Where a level's type was DECLARED — not just its name (names collide
|
|
848
|
-
// across levels and collections).
|
|
849
|
-
export interface LineageTypeRef {
|
|
850
|
-
collection: string;
|
|
851
|
-
path: string;
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
export interface LineageNode {
|
|
855
|
-
type: LineageTypeRef;
|
|
856
|
-
// the fields this level set itself — shadowed values stay with their
|
|
857
|
-
// writer
|
|
858
|
-
own: { [ key: string ]: LineageValue };
|
|
859
|
-
// the parent's id, null at the root
|
|
860
|
-
parent: string | null;
|
|
861
|
-
// construction args — present only when lineage() gets { args: true }
|
|
862
|
-
args?: LineageValue;
|
|
863
|
-
// opt-in metadata (e.g. __timestamp__) — present only when
|
|
864
|
-
// lineage() gets { props: [...] }
|
|
865
|
-
props?: { [ key: string ]: LineageValue };
|
|
866
|
-
}
|
|
867
|
-
|
|
868
|
-
// The lethe lineage graph: version "1", heads in argument order, every
|
|
869
|
-
// reachable instance deduplicated at any depth.
|
|
870
|
-
export interface LineageGraph {
|
|
871
|
-
version: '1';
|
|
872
|
-
heads: string[];
|
|
873
|
-
nodes: { [ id: string ]: LineageNode };
|
|
874
|
-
}
|
|
875
|
-
|
|
876
|
-
export interface LineageOptions {
|
|
877
|
-
/** include construction args as node.args */
|
|
878
|
-
args?: boolean;
|
|
879
|
-
/** opt-in getProps keys exported as node.props (e.g. [ '__timestamp__' ]) */
|
|
880
|
-
props?: string[];
|
|
881
|
-
}
|
|
882
|
-
|
|
883
|
-
// Sibling type accessor
|
|
884
|
-
export interface SiblingAccessor {
|
|
885
|
-
(SiblingTypeName: string): TypeClass | undefined;
|
|
886
|
-
[key: string]: TypeClass | undefined;
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
// Mnemonica instance methods interface (opt-in).
|
|
890
|
-
// These methods used to be auto-injected onto every instance. They are now
|
|
891
|
-
// available only when a type explicitly adds them to its prototype.
|
|
892
|
-
// Users can still use this interface to type their own root-level helpers.
|
|
893
|
-
export interface MnemonicaInstance<T extends object = object> {
|
|
894
|
-
extract(): Extracted<T>;
|
|
895
|
-
pick<K extends keyof T>(...keys: (K | K[])[]): { [P in K]: T[P] } & {};
|
|
896
|
-
pick(...keys: string[]): Record<string, unknown>;
|
|
897
|
-
parent(): object | undefined;
|
|
898
|
-
parent<K extends ParentPathOfInstance<this> & string>(
|
|
899
|
-
constructorLookupPath: K
|
|
900
|
-
): InstanceOfTypeRegistry<K> | undefined;
|
|
901
|
-
parent(constructorLookupPath: string): object | undefined;
|
|
902
|
-
readonly clone: this;
|
|
903
|
-
fork(...forkArgs: unknown[]): this;
|
|
904
|
-
exception(error: Error, ...args: unknown[]): Error;
|
|
905
|
-
readonly sibling: SiblingAccessor;
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
// Instance properties for __self__ reference
|
|
909
|
-
// This merges internal props with the instance methods
|
|
910
|
-
// Internal instance properties (non-enumerable)
|
|
911
|
-
// These are always present on instances but accessed via getProps/setProps
|
|
912
|
-
export type InstanceInternalProps = {
|
|
913
|
-
__proto_proto__: object;
|
|
914
|
-
__args__: unknown[];
|
|
915
|
-
__collection__: CollectionDef;
|
|
916
|
-
__subtypes__: Map<string, object>;
|
|
917
|
-
__type__: TypeDef;
|
|
918
|
-
__parent__: object;
|
|
919
|
-
__stack__?: string;
|
|
920
|
-
__creator__: InstanceCreatorContext;
|
|
921
|
-
__timestamp__: number;
|
|
922
|
-
};
|
|
923
|
-
|
|
924
|
-
// Combined Props type for internal use inside ./src
|
|
925
|
-
export type Props = InstanceInternalProps & {
|
|
926
|
-
__self__: InstanceInternalProps;
|
|
927
|
-
[key: string]: unknown;
|
|
928
|
-
};
|
|
815
|
+
this: unknown,
|
|
816
|
+
TypeNestedPath: K
|
|
817
|
+
): LookedUpConstructor<Registry, Extract<keyof Registry & string, `${Path}.${K}`>>;
|
|
818
|
+
}) & {
|
|
819
|
+
(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
820
|
+
};
|
|
929
821
|
|
|
930
|
-
//
|
|
931
|
-
// Instance methods (extract, pick, fork, etc.) are no longer auto-injected.
|
|
932
|
-
// The inline mapped type is used instead of the Flatten alias so that hover
|
|
933
|
-
// tooltips show the actual field object literal first, not "Flatten<{...}>".
|
|
934
|
-
export type InstanceResult<
|
|
935
|
-
N extends object,
|
|
936
|
-
> = { [K in keyof N]: N[K] };
|
|
822
|
+
// --- builder holders and collections ---------------------------------------
|
|
937
823
|
|
|
938
824
|
// The registry VALUE as public vocabulary: what a builder chain stores per
|
|
939
825
|
// defined path and what typed lookups read back. Interface (not alias) so
|
|
@@ -974,6 +860,13 @@ export interface AsyncRegistryEntry<
|
|
|
974
860
|
registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, Parent, F>): void;
|
|
975
861
|
}
|
|
976
862
|
|
|
863
|
+
// The dotted path of a type named `Name` defined on a holder at `Path`:
|
|
864
|
+
// the bare name on a root holder (`Path = ''`), `${Path}.${Name}` below it.
|
|
865
|
+
// The default of every define()/lazy() overload's ChildPath; resolved at the
|
|
866
|
+
// call site, so consumers see the literal path, never this name.
|
|
867
|
+
export type ChildPathOf<Path extends string, Name extends string> =
|
|
868
|
+
Path extends '' ? Name : `${Path}.${Name}`;
|
|
869
|
+
|
|
977
870
|
// Registry holder base - provides the accumulating .define() method.
|
|
978
871
|
// `Parent` tracks the instance type of the last defined constructor, so chained
|
|
979
872
|
// subtypes get the correct merged `Proto<Parent, N>` instance type.
|
|
@@ -999,7 +892,7 @@ export interface RegistryHolderBase<
|
|
|
999
892
|
N extends object,
|
|
1000
893
|
Args extends unknown[] = unknown[],
|
|
1001
894
|
F extends Proto<Parent, N> = Proto<Parent, N>,
|
|
1002
|
-
ChildPath extends string = Path
|
|
895
|
+
ChildPath extends string = ChildPathOf<Path, Name>
|
|
1003
896
|
>(
|
|
1004
897
|
this: RegistryHolderBase<T, Parent, Path>,
|
|
1005
898
|
TypeName: Name,
|
|
@@ -1020,7 +913,7 @@ export interface RegistryHolderBase<
|
|
|
1020
913
|
N extends object,
|
|
1021
914
|
Args extends unknown[] = unknown[],
|
|
1022
915
|
F extends Proto<Parent, N> = Proto<Parent, N>,
|
|
1023
|
-
ChildPath extends string = Path
|
|
916
|
+
ChildPath extends string = ChildPathOf<Path, Name>
|
|
1024
917
|
>(
|
|
1025
918
|
this: RegistryHolderBase<T, Parent, Path>,
|
|
1026
919
|
TypeName: Name,
|
|
@@ -1042,7 +935,7 @@ export interface RegistryHolderBase<
|
|
|
1042
935
|
N extends object,
|
|
1043
936
|
Args extends unknown[],
|
|
1044
937
|
F extends Proto<Parent, N> = Proto<Parent, N>,
|
|
1045
|
-
ChildPath extends string = Path
|
|
938
|
+
ChildPath extends string = ChildPathOf<Path, Name>
|
|
1046
939
|
>(
|
|
1047
940
|
this: RegistryHolderBase<T, Parent, Path>,
|
|
1048
941
|
TypeName: Name,
|
|
@@ -1065,7 +958,7 @@ export interface RegistryHolderBase<
|
|
|
1065
958
|
N extends object,
|
|
1066
959
|
Args extends unknown[],
|
|
1067
960
|
F extends Proto<Parent, N> = Proto<Parent, N>,
|
|
1068
|
-
ChildPath extends string = Path
|
|
961
|
+
ChildPath extends string = ChildPathOf<Path, Name>
|
|
1069
962
|
>(
|
|
1070
963
|
this: RegistryHolderBase<T, Parent, Path>,
|
|
1071
964
|
TypeName: Name,
|
|
@@ -1113,14 +1006,12 @@ export interface IDefinitorInstance<
|
|
|
1113
1006
|
TypeName: string;
|
|
1114
1007
|
prototype: N;
|
|
1115
1008
|
|
|
1116
|
-
|
|
1117
1009
|
// the line below declares a `new ...` invocation,
|
|
1118
1010
|
// so makes interface constructible
|
|
1119
1011
|
// new(...args: unknown[]): { [key in keyof R]: R[key] };
|
|
1120
1012
|
/** @throws {CreationError<R>} when construction fails */
|
|
1121
1013
|
new(...args: Args): R;
|
|
1122
1014
|
|
|
1123
|
-
|
|
1124
1015
|
// TODO: need check if line below works
|
|
1125
1016
|
// also TS hinting check as well
|
|
1126
1017
|
// the line below should make is a @decorate decorator working
|
|
@@ -1199,19 +1090,6 @@ export interface TypesCollection<
|
|
|
1199
1090
|
[key: string]: unknown;
|
|
1200
1091
|
}
|
|
1201
1092
|
|
|
1202
|
-
// Shared interface for objects that support hooks (TypeDef and CollectionDef)
|
|
1203
|
-
export interface Hookable {
|
|
1204
|
-
hooks: Record<string, Set<hook>>;
|
|
1205
|
-
invokeHook(hookType: hooksTypes, opts: hooksOpts): Set<unknown>;
|
|
1206
|
-
// Collection-level registration: the payload split by hookType holds
|
|
1207
|
-
// (preCreation has no inheritedInstance/throwModificationError; post
|
|
1208
|
-
// hooks carry both, required) but P/T stay `object` — a collection
|
|
1209
|
-
// mixes every registered type, so narrowing to a concrete instance
|
|
1210
|
-
// happens in the callback.
|
|
1211
|
-
registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT>): void;
|
|
1212
|
-
registerFlowChecker(cb: FlowChecker): void;
|
|
1213
|
-
}
|
|
1214
|
-
|
|
1215
1093
|
// createTypesCollection function type
|
|
1216
1094
|
export type CreateTypesCollectionFunction =
|
|
1217
1095
|
<T extends object = {}, Parent extends object = object>(
|
|
@@ -1221,59 +1099,6 @@ export type CreateTypesCollectionFunction =
|
|
|
1221
1099
|
// Type class - base type constructor
|
|
1222
1100
|
export type TypeClass = IDefinitorInstance<object>;
|
|
1223
1101
|
|
|
1224
|
-
// Generic registry map used by typed collections and the mnemonica builder.
|
|
1225
|
-
// Keys are dotted type paths; values are mnemonica-compatible constructors.
|
|
1226
|
-
export type TypeRegistryMap = Record<string, TypeConstructorBase>;
|
|
1227
|
-
|
|
1228
|
-
// Mnemonica constructor — the constructor function returned by constructHandler()
|
|
1229
|
-
// Has SymbolConstructorName attached and is both newable and callable
|
|
1230
|
-
export interface MnemonicaConstructor extends NewableFunction {
|
|
1231
|
-
new (...args: unknown[]): object;
|
|
1232
|
-
(this: object, ...args: unknown[]): unknown;
|
|
1233
|
-
[key: symbol]: unknown;
|
|
1234
|
-
}
|
|
1235
|
-
|
|
1236
|
-
// Type descriptor instance — internal shape of TypeDescriptor objects
|
|
1237
|
-
export interface TypeDescriptorDefine extends CallableFunction {
|
|
1238
|
-
(
|
|
1239
|
-
TypeOrTypeName: string | DefineNewableOrCallable,
|
|
1240
|
-
constructHandlerOrConfig?: DefineNewableOrCallable | object,
|
|
1241
|
-
config?: object
|
|
1242
|
-
): TypeClass;
|
|
1243
|
-
}
|
|
1244
|
-
|
|
1245
|
-
export interface TypeDescriptorLazy extends CallableFunction {
|
|
1246
|
-
(
|
|
1247
|
-
TypeName: string,
|
|
1248
|
-
getter: LazyDef<object>,
|
|
1249
|
-
config?: object
|
|
1250
|
-
): TypeClass;
|
|
1251
|
-
}
|
|
1252
|
-
|
|
1253
|
-
export interface TypeDescriptorLookup extends CallableFunction {
|
|
1254
|
-
(TypeNestedPath: string): TypeClass | undefined;
|
|
1255
|
-
}
|
|
1256
|
-
|
|
1257
|
-
export type TypeDescriptorInstance = {
|
|
1258
|
-
define: TypeDescriptorDefine;
|
|
1259
|
-
lazy: TypeDescriptorLazy;
|
|
1260
|
-
decorate: (config?: constructorOptions) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U>;
|
|
1261
|
-
lookup: TypeDescriptorLookup;
|
|
1262
|
-
subtypes: Map<string, object>;
|
|
1263
|
-
TypeName: string;
|
|
1264
|
-
collection: CollectionDef;
|
|
1265
|
-
};
|
|
1266
|
-
|
|
1267
|
-
// Constructor type for decorate function.
|
|
1268
|
-
// Parameters are `never[]` for the same reason as AnyConstructor: under
|
|
1269
|
-
// strictFunctionTypes an `unknown[]` constraint rejects classes with precise
|
|
1270
|
-
// constructor parameters, so `@decorate()` could not be applied to them.
|
|
1271
|
-
export type Constructor<T = object> = new (...args: never[]) => T;
|
|
1272
|
-
|
|
1273
|
-
// Name of a constructor as a string literal type.
|
|
1274
|
-
export type ConstructorName<T extends Constructor<object>> =
|
|
1275
|
-
T extends { name: infer N } ? N extends string ? N : string : string;
|
|
1276
|
-
|
|
1277
1102
|
// Decorated class type - includes call signature for decorator pattern and
|
|
1278
1103
|
// aligns with `IDefinitorInstance` so decorated classes expose the same
|
|
1279
1104
|
// constructor/lookup surface as builder-created types.
|
|
@@ -1294,6 +1119,138 @@ export type DecoratedClass<
|
|
|
1294
1119
|
lookup: TypeLookup<Registry>;
|
|
1295
1120
|
};
|
|
1296
1121
|
|
|
1122
|
+
// --- instance shapes and utils ---------------------------------------------
|
|
1123
|
+
|
|
1124
|
+
// Props type for getProps/setProps
|
|
1125
|
+
export type PropsType = Record<string, unknown>;
|
|
1126
|
+
|
|
1127
|
+
// A utility method as exposed on the `utils` collection — the
|
|
1128
|
+
// index-signature fallback for utilities beyond the declared set. Plain
|
|
1129
|
+
// callables: any argument list, unknown result.
|
|
1130
|
+
export interface UtilFunction extends CallableFunction {
|
|
1131
|
+
(...args: never[]): unknown;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
// The bound fork invoker that instance.fork() returns: consumes fork
|
|
1135
|
+
// arguments, produces a fresh instance of the forked type.
|
|
1136
|
+
export interface ForkInvoker<T extends object> extends CallableFunction {
|
|
1137
|
+
(this: object | void, ...forkArgs: unknown[]): T;
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
/**
|
|
1141
|
+
* Proto merge type - combines parent and child types without property conflicts.
|
|
1142
|
+
*
|
|
1143
|
+
* When a subtype is defined from a parent instance, we want the child properties
|
|
1144
|
+
* to take precedence. `Exclude<keyof P, keyof T>` removes overlapping keys from P,
|
|
1145
|
+
* so `Pick<P, ...>` only brings in parent properties that don't clash with child.
|
|
1146
|
+
*
|
|
1147
|
+
* Result: child T gets all its own props, plus parent's non-overlapping props.
|
|
1148
|
+
*/
|
|
1149
|
+
// When the parent contributes no properties, collapse to the child type so
|
|
1150
|
+
// hover tooltips stay readable.
|
|
1151
|
+
export type Proto<P extends object, T extends object> =
|
|
1152
|
+
[Exclude<keyof P, keyof T>] extends [never]
|
|
1153
|
+
? T
|
|
1154
|
+
: T & Pick<P, Exclude<keyof P, keyof T>>;
|
|
1155
|
+
|
|
1156
|
+
// export type ProtoFlat<
|
|
1157
|
+
// P extends object,
|
|
1158
|
+
// T extends object,
|
|
1159
|
+
// F extends Proto<P, T> = Proto<P, T>
|
|
1160
|
+
// > = { [key in keyof F]: F[key]} ;
|
|
1161
|
+
|
|
1162
|
+
export type ProtoFlat<
|
|
1163
|
+
P extends object,
|
|
1164
|
+
T extends object,
|
|
1165
|
+
L extends Exclude<keyof P, keyof T> = Exclude<keyof P, keyof T>,
|
|
1166
|
+
> = {
|
|
1167
|
+
[key in keyof T]: T[key];
|
|
1168
|
+
} & {
|
|
1169
|
+
[key in L]: P[key];
|
|
1170
|
+
};
|
|
1171
|
+
|
|
1172
|
+
// Mnemonica instance method keys. These are filtered out of extracted/merged
|
|
1173
|
+
// field-only views because they belong to the prototype, not to the user's
|
|
1174
|
+
// enumerable data properties.
|
|
1175
|
+
export type MnemonicaInstanceMethodKeys =
|
|
1176
|
+
'extract' | 'pick' | 'parent' | 'clone' | 'fork' | 'exception' | 'sibling';
|
|
1177
|
+
|
|
1178
|
+
// Extracted<T>: the shape returned by utils.extract(instance).
|
|
1179
|
+
// It contains the enumerable string-keyed user properties of the instance,
|
|
1180
|
+
// omitting MnemonicaInstance methods (extract, pick, parent, clone, fork,
|
|
1181
|
+
// exception, sibling). Optionality is preserved from the source type.
|
|
1182
|
+
// The `& {}` trick forces TypeScript to expand the alias in hover tooltips
|
|
1183
|
+
// so it renders as a plain object literal instead of "Extracted<{...}>".
|
|
1184
|
+
export type Extracted<T extends object> = {
|
|
1185
|
+
[K in keyof T as K extends string ? (K extends MnemonicaInstanceMethodKeys ? never : K) : never]: T[K];
|
|
1186
|
+
} & {};
|
|
1187
|
+
|
|
1188
|
+
// Parsed<T>: the shape returned by utils.parse(instance).
|
|
1189
|
+
// It is a one-level snapshot of the instance's prototype chain.
|
|
1190
|
+
// `props` contains the instance's enumerable user properties;
|
|
1191
|
+
// `joint` contains enumerable properties copied from the immediate prototype;
|
|
1192
|
+
// `parent` is the parent INSTANCE — the same object utils.parent(instance)
|
|
1193
|
+
// returns; null for no parent. Not recursively parsed — walking the whole
|
|
1194
|
+
// lineage level by level is what utils.deepParse does — this function stays
|
|
1195
|
+
// the one-level primitive.
|
|
1196
|
+
// The three "no parent" readings, stated once:
|
|
1197
|
+
// parent == null — no parent at all (covers both below);
|
|
1198
|
+
// parent === null — the end of a REAL chain (a root instance);
|
|
1199
|
+
// parent === undefined — nothing was given (utils.parse(null)).
|
|
1200
|
+
export type Parsed<T extends object> = {
|
|
1201
|
+
name: string;
|
|
1202
|
+
props: Extracted<T>;
|
|
1203
|
+
self: T;
|
|
1204
|
+
proto: object;
|
|
1205
|
+
joint: Record<string, unknown>;
|
|
1206
|
+
parent: object | null;
|
|
1207
|
+
};
|
|
1208
|
+
|
|
1209
|
+
// EmptyParsed: the shape utils.parse(null) returns — the one non-object
|
|
1210
|
+
// input parse accepts. "Nothing was given": every field is its empty value,
|
|
1211
|
+
// and parent is undefined (see the readings above).
|
|
1212
|
+
export interface EmptyParsed {
|
|
1213
|
+
name: undefined;
|
|
1214
|
+
props: {};
|
|
1215
|
+
self: null;
|
|
1216
|
+
proto: undefined;
|
|
1217
|
+
joint: {};
|
|
1218
|
+
parent: undefined;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
// Sibling type accessor
|
|
1222
|
+
export interface SiblingAccessor {
|
|
1223
|
+
(SiblingTypeName: string): TypeClass | undefined;
|
|
1224
|
+
[key: string]: TypeClass | undefined;
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
// Mnemonica instance methods interface (opt-in).
|
|
1228
|
+
// These methods used to be auto-injected onto every instance. They are now
|
|
1229
|
+
// available only when a type explicitly adds them to its prototype.
|
|
1230
|
+
// Users can still use this interface to type their own root-level helpers.
|
|
1231
|
+
export interface MnemonicaInstance<T extends object = object> {
|
|
1232
|
+
extract(): Extracted<T>;
|
|
1233
|
+
pick<K extends keyof T>(...keys: (K | K[])[]): { [P in K]: T[P] } & {};
|
|
1234
|
+
pick(...keys: string[]): Record<string, unknown>;
|
|
1235
|
+
parent(): object | undefined;
|
|
1236
|
+
parent<K extends ParentPathOfInstance<this> & string>(
|
|
1237
|
+
constructorLookupPath: K
|
|
1238
|
+
): InstanceOfTypeRegistry<K> | undefined;
|
|
1239
|
+
parent(constructorLookupPath: string): object | undefined;
|
|
1240
|
+
readonly clone: this;
|
|
1241
|
+
fork(...forkArgs: unknown[]): this;
|
|
1242
|
+
exception(error: Error, ...args: unknown[]): Error;
|
|
1243
|
+
readonly sibling: SiblingAccessor;
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
// Combined instance type: plain object with the user's fields only.
|
|
1247
|
+
// Instance methods (extract, pick, fork, etc.) are no longer auto-injected.
|
|
1248
|
+
// The inline mapped type is used instead of the Flatten alias so that hover
|
|
1249
|
+
// tooltips show the actual field object literal first, not "Flatten<{...}>".
|
|
1250
|
+
export type InstanceResult<
|
|
1251
|
+
N extends object,
|
|
1252
|
+
> = { [K in keyof N]: N[K] };
|
|
1253
|
+
|
|
1297
1254
|
// Helper: merge parent entity (E) and child constructor instance (T) into a
|
|
1298
1255
|
// single flat object type, filtering out MnemonicaInstance method names.
|
|
1299
1256
|
// Used by apply/call/bind.
|
|
@@ -1337,7 +1294,11 @@ export interface UtilsCollection {
|
|
|
1337
1294
|
clone<T extends object>(instance: T): T;
|
|
1338
1295
|
fork<T extends object>(instance: T): ForkInvoker<T>;
|
|
1339
1296
|
sibling(instance: object): SiblingAccessor;
|
|
1340
|
-
|
|
1297
|
+
// the second argument picks the result: the ordered names for true, a
|
|
1298
|
+
// { name : true } lookup object by default
|
|
1299
|
+
collectConstructors(instance: object, asSequence: true): string[];
|
|
1300
|
+
collectConstructors(instance: object, asSequence?: false): Record<string, boolean>;
|
|
1301
|
+
collectConstructors(instance: object, asSequence?: boolean): string[] | Record<string, boolean>;
|
|
1341
1302
|
merge<A extends object, B extends object>(
|
|
1342
1303
|
a: A,
|
|
1343
1304
|
b: B,
|
|
@@ -1357,6 +1318,58 @@ export interface UtilsCollection {
|
|
|
1357
1318
|
[key: string]: UtilFunction;
|
|
1358
1319
|
}
|
|
1359
1320
|
|
|
1321
|
+
// --- lineage (the lethe cross-language export) -----------------------------
|
|
1322
|
+
|
|
1323
|
+
// A JSON-safe value in the lethe lineage graph: instances export as $ref,
|
|
1324
|
+
// non-JSON values as tagged placeholders — never an error.
|
|
1325
|
+
export type LineageValue =
|
|
1326
|
+
| null
|
|
1327
|
+
| boolean
|
|
1328
|
+
| string
|
|
1329
|
+
| number
|
|
1330
|
+
| LineageValue[]
|
|
1331
|
+
| { $ref: string }
|
|
1332
|
+
| { '$mnemonica': 'unsupported'; kind: string }
|
|
1333
|
+
| { [ key: string ]: LineageValue };
|
|
1334
|
+
|
|
1335
|
+
// Where a level's type was DECLARED — not just its name (names collide
|
|
1336
|
+
// across levels and collections).
|
|
1337
|
+
export interface LineageTypeRef {
|
|
1338
|
+
collection: string;
|
|
1339
|
+
path: string;
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
export interface LineageNode {
|
|
1343
|
+
type: LineageTypeRef;
|
|
1344
|
+
// the fields this level set itself — shadowed values stay with their
|
|
1345
|
+
// writer
|
|
1346
|
+
own: { [ key: string ]: LineageValue };
|
|
1347
|
+
// the parent's id, null at the root
|
|
1348
|
+
parent: string | null;
|
|
1349
|
+
// construction args — present only when lineage() gets { args: true }
|
|
1350
|
+
args?: LineageValue;
|
|
1351
|
+
// opt-in metadata (e.g. __timestamp__) — present only when
|
|
1352
|
+
// lineage() gets { props: [...] }
|
|
1353
|
+
props?: { [ key: string ]: LineageValue };
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
// The lethe lineage graph: version "1", heads in argument order, every
|
|
1357
|
+
// reachable instance deduplicated at any depth.
|
|
1358
|
+
export interface LineageGraph {
|
|
1359
|
+
version: '1';
|
|
1360
|
+
heads: string[];
|
|
1361
|
+
nodes: { [ id: string ]: LineageNode };
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
export interface LineageOptions {
|
|
1365
|
+
/** include construction args as node.args */
|
|
1366
|
+
args?: boolean;
|
|
1367
|
+
/** opt-in getProps keys exported as node.props (e.g. [ '__timestamp__' ]) */
|
|
1368
|
+
props?: string[];
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
// --- the module object -----------------------------------------------------
|
|
1372
|
+
|
|
1360
1373
|
// Main mnemonica module interface - represents the exported module object.
|
|
1361
1374
|
// The optional Registry generic lets the object act as a typed registry holder
|
|
1362
1375
|
// when chaining .define() calls.
|