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.
- package/.ai/AGENTS.md +31 -18
- package/.ai/DEBUG.md +7 -8
- package/.ai/ONBOARDING.md +24 -7
- package/.ai/PROTOTYPE-CHAIN.md +33 -2
- package/.ai/{ask/AGENTS.md → mode-ask.md} +1 -1
- package/.ai/{orchestrator/AGENTS.md → mode-orchestrator.md} +2 -19
- package/{docs → .ai}/performance-vs-security.md +1 -1
- package/.ai/{rules-skill/async-constructors.md → rules-async-constructors.md} +146 -0
- package/.ai/{rules/CODING.md → rules-coding.md} +19 -2
- package/.ai/{rules/CONTEXT-CONDENSING.md → rules-context-condensing.md} +2 -2
- package/.ai/{rules-skill/contributing.md → rules-contributing.md} +35 -0
- package/.ai/{rules-skill/define-patterns.md → rules-define-patterns.md} +39 -0
- package/.ai/{rules-skill/instance-methods.md → rules-instance-methods.md} +1 -1
- package/.ai/rules-reminders.md +19 -0
- package/.ai/{rules-skill/type-system.md → rules-type-system.md} +1 -1
- package/{docs → .ai}/theory-of-operations.md +4 -4
- package/AGENTS.md +76 -32
- package/CONTRIBUTING.md +4 -18
- package/FOR_HUMANS.md +123 -83
- package/README.md +65 -28
- package/SKILL.md +27 -22
- package/build/api/errors/index.js +7 -5
- package/build/api/index.d.ts +1 -0
- package/build/api/index.js +2 -1
- package/build/api/types/InstanceCreator.js +16 -5
- package/build/api/types/Mnemosyne.d.ts +3 -14
- package/build/api/types/Mnemosyne.js +21 -8
- package/build/api/types/Props.js +1 -2
- package/build/api/types/TypeProxy.d.ts +1 -1
- package/build/api/types/TypeProxy.js +9 -9
- package/build/api/types/compileNewModificatorFunctionBody.js +3 -5
- package/build/api/types/index.d.ts +5 -0
- package/build/api/types/index.js +77 -33
- package/build/api/utils/index.d.ts +1 -0
- package/build/api/utils/index.js +13 -3
- package/build/descriptors/types/index.js +46 -2
- package/build/index.d.ts +14 -3
- package/build/index.js +34 -7
- package/build/types/index.d.ts +24 -4
- package/build/types/index.js +1 -1
- package/build/utils/parent.js +35 -5
- package/docs/ai-learning-trajectory.md +2 -3
- package/docs/async-constructors.md +1 -1
- package/docs/decorate.md +123 -0
- package/docs/empathy-in-ai.md +2 -3
- package/docs/hott-primer.md +1 -1
- package/docs/prototype-chain.md +4 -0
- package/docs/purpose.md +3 -3
- package/{.ai/TACTICA-DEEP-DIVE.md → docs/tactica-deep-dive.md} +44 -1
- package/docs/tactica-pattern.md +1 -2
- package/docs/typed-lookup.md +183 -50
- package/package.json +8 -8
- package/src/api/errors/index.ts +10 -4
- package/src/api/index.ts +2 -8
- package/src/api/types/InstanceCreator.ts +28 -6
- package/src/api/types/Mnemosyne.ts +34 -15
- package/src/api/types/Props.ts +4 -4
- package/src/api/types/TypeProxy.ts +10 -9
- package/src/api/types/compileNewModificatorFunctionBody.ts +12 -4
- package/src/api/types/index.ts +136 -69
- package/src/api/utils/index.ts +27 -9
- package/src/descriptors/types/index.ts +81 -3
- package/src/index.ts +150 -7
- package/src/types/index.ts +120 -10
- package/src/utils/parent.ts +48 -4
- package/.ai/CODE.md +0 -111
- package/.ai/TACTICA-RULES.md +0 -103
- package/.ai/async_init.md +0 -94
- package/.ai/rules/REMINDERS.md +0 -63
- package/.ai/rules-skill/lookup-typed.md +0 -82
- package/.ai/rules-skill/proxy-architecture.md +0 -63
- package/.ai/task-templates/new-feature.md +0 -46
- /package/.ai/{rules-skill/code-style.md → rules-code-style.md} +0 -0
- /package/.ai/{rules-skill/ecosystem.md → rules-ecosystem.md} +0 -0
- /package/.ai/{rules-skill/error-system.md → rules-error-system.md} +0 -0
- /package/.ai/{rules-skill/hooks.md → rules-hooks.md} +0 -0
- /package/.ai/{rules-skill/philosophy.md → rules-philosophy.md} +0 -0
- /package/.ai/{rules-skill/testing.md → rules-testing.md} +0 -0
|
@@ -179,7 +179,7 @@ console.log(child instanceof AsyncChild); // true
|
|
|
179
179
|
|
|
180
180
|
When a constructor returns a Promise, the initial result of `new Constructor()` is that Promise. mnemonica's async pipeline waits for it to resolve, then must run validation and lifecycle hooks. But it must not run them again if the resolved instance is ever re-examined.
|
|
181
181
|
|
|
182
|
-
`_setSelf(instance)` solves this by adding a `__self__` getter to the instance's internal props that returns the instance itself
|
|
182
|
+
`_setSelf(instance)` solves this by adding a `__self__` getter to the instance's internal props that returns the instance itself. `makeAwaiter` retrieves those props through the same prototype-chain walk `getProps` uses (down to the instance's memory layer) and checks:
|
|
183
183
|
|
|
184
184
|
```js
|
|
185
185
|
if (props.__self__ !== self.inheritedInstance) {
|
package/docs/decorate.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# `@decorate()` — class-based type definitions
|
|
2
|
+
|
|
3
|
+
`@decorate()` registers a class as a mnemonica type. At runtime it is exactly
|
|
4
|
+
equivalent to `define()` — the same constructor is registered, the same
|
|
5
|
+
prototype chain is built. It exists for codebases that prefer class syntax.
|
|
6
|
+
|
|
7
|
+
**This is not the default path.** `@decorate()` requires `TypeRegistry`
|
|
8
|
+
augmentation — [`@mnemonica/tactica`](https://www.npmjs.com/package/@mnemonica/tactica)
|
|
9
|
+
or a hand-written file — to be typed. If you are not using Tactica, use
|
|
10
|
+
[builder mode](./typed-lookup.md) instead.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Why it needs Tactica
|
|
15
|
+
|
|
16
|
+
TypeScript never applies a class decorator's return type to the class binding's
|
|
17
|
+
declared type. Whatever `@decorate()` returns, the name you declared stays
|
|
18
|
+
plain `typeof YourClass` for every consumer — the enriched constructor type is
|
|
19
|
+
discarded at the decoration site. There is no channel for a local,
|
|
20
|
+
value-carried registry (which is what builder mode relies on), so the only way
|
|
21
|
+
to recover the types is the global `TypeRegistry` interface — and something
|
|
22
|
+
has to populate it. That something is Tactica: it scans your `@decorate()` and
|
|
23
|
+
`define()` calls and generates the augmentation for you.
|
|
24
|
+
|
|
25
|
+
For the full explanation of the two type-system paths, see
|
|
26
|
+
[`docs/typed-lookup.md`](./typed-lookup.md).
|
|
27
|
+
|
|
28
|
+
## Setup
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install mnemonica @mnemonica/tactica
|
|
32
|
+
npx tactica # scans define()/decorate() calls, generates .tactica/
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Enable decorators in `tsconfig.json`:
|
|
36
|
+
|
|
37
|
+
```json
|
|
38
|
+
{
|
|
39
|
+
"compilerOptions": {
|
|
40
|
+
"experimentalDecorators": true
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Import the generated augmentation once (e.g. in your entry file):
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import './.tactica/registry';
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Basic usage
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { decorate, lookup } from 'mnemonica';
|
|
55
|
+
|
|
56
|
+
@decorate()
|
|
57
|
+
class UserType {
|
|
58
|
+
name: string;
|
|
59
|
+
email: string;
|
|
60
|
+
constructor (data: { name: string; email: string }) {
|
|
61
|
+
Object.assign(this, data);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const user = new UserType({ name: 'John', email: 'john@example.com' });
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Subtypes: `@decorate(Parent)`
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
@decorate(UserType)
|
|
72
|
+
class AdminType {
|
|
73
|
+
role: string = 'admin';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Subtypes are still constructed from a parent INSTANCE — same rule as define()
|
|
77
|
+
const admin = new user.AdminType();
|
|
78
|
+
|
|
79
|
+
admin.name; // 'John' (inherited)
|
|
80
|
+
admin.role; // 'admin' (own property)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## With configuration
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
@decorate({ strictChain: false, blockErrors: true })
|
|
87
|
+
class ConfiguredClass {
|
|
88
|
+
field: number = 123;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
@decorate(UserType, { strictChain: false })
|
|
92
|
+
class ConfiguredChildClass {
|
|
93
|
+
field: number = 123;
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Typed retrieval
|
|
98
|
+
|
|
99
|
+
Because the class binding itself stays untyped (see above), retrieve the typed
|
|
100
|
+
constructor through `lookup()` once Tactica has augmented the registry:
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
const AdminCtor = lookup('UserType.AdminType');
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
**Advanced note:** after a class is decorated with `@decorate()`, it can be
|
|
107
|
+
used as a decorator for nested types (may require `@ts-ignore` due to
|
|
108
|
+
TypeScript limitations with callable class types).
|
|
109
|
+
|
|
110
|
+
## Combining with TypeØmatica
|
|
111
|
+
|
|
112
|
+
`@decorate()` composes with [`typeomatica`](https://www.npmjs.com/package/typeomatica)'s
|
|
113
|
+
`@Strict()` runtime type enforcement. Decorator order matters — `@Strict`
|
|
114
|
+
must be applied first (bottom position). See
|
|
115
|
+
[`docs/typeomatica.md`](./typeomatica.md) for the combined patterns.
|
|
116
|
+
|
|
117
|
+
## When not to use `@decorate()`
|
|
118
|
+
|
|
119
|
+
- **No Tactica in the project?** Use builder mode — typed with zero tooling.
|
|
120
|
+
- **Only a few types?** Builder mode plus the one-line `RegistryOf` bridge
|
|
121
|
+
covers free `lookup()` too — see [`docs/typed-lookup.md`](./typed-lookup.md).
|
|
122
|
+
- **Class-heavy codebase already on Tactica?** Then `@decorate()` is the
|
|
123
|
+
natural fit — that is what it is for.
|
package/docs/empathy-in-ai.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
This document connects mnemonica's data-flow model to a specific AI design problem: **how an agent understands another agent's state**. The connection is not metaphorical decoration. It follows directly from the library's structure.
|
|
4
4
|
|
|
5
|
-
> If you want the deeper theory first, see the HoTT framing in [`docs/hott-primer.md`](./hott-primer.md) and the original vision in [
|
|
5
|
+
> If you want the deeper theory first, see the HoTT framing in [`docs/hott-primer.md`](./hott-primer.md) and the original vision in [the 2019 inheritance essay](https://github.com/mythographica/stash/blob/master/inheritance.md). This document stays at the API level.
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
@@ -166,5 +166,4 @@ What it provides is a data model where context and lineage are first-class. High
|
|
|
166
166
|
|
|
167
167
|
- [`docs/hott-primer.md`](./hott-primer.md) — formal background on paths, identity, and transport
|
|
168
168
|
- [`docs/purpose.md`](./purpose.md) — data flow vs control flow, and what mnemonica does not do
|
|
169
|
-
- [
|
|
170
|
-
- [`../../hott/theory/MEMORY-AND-ESSENCE.md`](../../hott/theory/MEMORY-AND-ESSENCE.md) — the HoTT treatment of identity as path-connectedness
|
|
169
|
+
- [Inheritance in JavaScript — the original 2019 essay](https://github.com/mythographica/stash/blob/master/inheritance.md) — the vision that posed the empathy question
|
package/docs/hott-primer.md
CHANGED
|
@@ -27,7 +27,7 @@ The `bind` operation is `new instance.SubType(args)`: it takes a value in contex
|
|
|
27
27
|
|
|
28
28
|
**Univalence Axiom.** In HoTT, equivalence and identity coincide — there is no gap between "same structure" and "same thing." Mnemonica's nominal typing makes this intuition natural: two instances with identical property shapes but different constructors are *different types*. The constructor name (frozen at `define()` time) is the identity, not the shape. This is an **analogy** — mnemonica does not implement the univalence axiom formally, but the intuition that "identity is determined by constructor, not structure" is HoTT-inspired.
|
|
29
29
|
|
|
30
|
-
**Path Types.** In HoTT, equality is a type. `Id(a, b)` is the type of *paths* witnessing that `a` and `b` are equal. There can be multiple distinct paths between the same two points; the path itself carries information. In mnemonica, the chain from an instance back through `
|
|
30
|
+
**Path Types.** In HoTT, equality is a type. `Id(a, b)` is the type of *paths* witnessing that `a` and `b` are equal. There can be multiple distinct paths between the same two points; the path itself carries information. In mnemonica, the chain from an instance back through successive `Object.getPrototypeOf` links to the root type IS its identity-path. The `__args__` along the way parameterize that path. This correspondence is **structurally exact**.
|
|
31
31
|
|
|
32
32
|
**Higher Inductive Types (HITs).** A type defined by its constructors AND by paths between values. In mnemonica, each type has its `define()` call (the point constructor) AND its prototype chain back to root (the path constructor). The Trie structure can be **viewed as** a HIT. This is a productive analogy, not a formal encoding — mnemonica does not implement path constructors as first-class operations.
|
|
33
33
|
|
package/docs/prototype-chain.md
CHANGED
|
@@ -82,6 +82,10 @@ This is the invisible layer that makes mnemonica work. It:
|
|
|
82
82
|
|
|
83
83
|
Because the props live in a `WeakMap` attached to the memory layer, they do **not** show up in `Object.keys(instance)` or `JSON.stringify(instance)`.
|
|
84
84
|
|
|
85
|
+
The memory layer also keeps the metadata lookup deterministic: the `WeakMap` is keyed by an internal object identity, not by any name or property that user code could replace or pollute. This is not a security boundary — JavaScript offers none within a realm — but accidental interference cannot redirect the lookup.
|
|
86
|
+
|
|
87
|
+
**Note:** `getProps(instance)` returns the *live* internal props object, not a copy — mutating it mutates the instance’s construction record. (Once `setProps` has been used on the instance, `getProps` returns a merged copy instead, and mutations on it do not persist.)
|
|
88
|
+
|
|
85
89
|
For async constructors, the memory layer also receives a `__self__` marker after the Promise resolves. This lets mnemonica defer finalization until the async work is done without running validation and hooks twice.
|
|
86
90
|
|
|
87
91
|
---
|
package/docs/purpose.md
CHANGED
|
@@ -319,7 +319,7 @@ const instance = Object.create(proto); // Pure prototype, no Object inheritance
|
|
|
319
319
|
|
|
320
320
|
**Security Benefits**:
|
|
321
321
|
1. **No prototype pollution attacks** - `Object.prototype` mutations don't affect Mnemonica instances
|
|
322
|
-
2. **No `__proto__`
|
|
322
|
+
2. **No injection via the legacy `__proto__` accessor** - Instances don't inherit from Object
|
|
323
323
|
3. **Isolated namespace** - Properties like `constructor`, `toString`, `valueOf` don't exist unless explicitly defined
|
|
324
324
|
4. **Clean property enumeration** - `for...in` loops don't see inherited Object properties
|
|
325
325
|
|
|
@@ -552,8 +552,8 @@ const Child = Parent.define('Child', function() {});
|
|
|
552
552
|
|
|
553
553
|
const child = new Child({});
|
|
554
554
|
// child is a point
|
|
555
|
-
// child
|
|
556
|
-
// Child.prototype
|
|
555
|
+
// Object.getPrototypeOf(child) → Child.prototype is a path
|
|
556
|
+
// Object.getPrototypeOf(Child.prototype) → Parent.prototype is another path
|
|
557
557
|
// And so on...
|
|
558
558
|
```
|
|
559
559
|
|
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
> **Project-agnostic reference.** This document applies to every project using mnemonica with tactica.
|
|
4
4
|
> Read it when you are tempted to write `as unknown as` with mnemonica types.
|
|
5
5
|
|
|
6
|
+
> **Do you need tactica at all?** For builder-mode projects (`mnemonica.define(...)`
|
|
7
|
+
> or `createTypesCollection()`), the one-line `RegistryOf` bridge makes free
|
|
8
|
+
> `lookup()` fully typed with no codegen — see
|
|
9
|
+
> [`docs/typed-lookup.md`](../docs/typed-lookup.md). Tactica remains the answer
|
|
10
|
+
> when you need `@decorate()` typed, or when a large codebase already uses free
|
|
11
|
+
> `define()` calls throughout.
|
|
12
|
+
|
|
6
13
|
---
|
|
7
14
|
|
|
8
15
|
## Table of Contents
|
|
@@ -210,7 +217,18 @@ declare module 'mnemonica' {
|
|
|
210
217
|
|
|
211
218
|
After this augmentation, `TypeRegistry` contains the `'RequestData'` key. Any code that imports from `'mnemonica'` sees the augmented `TypeRegistry`.
|
|
212
219
|
|
|
213
|
-
### 4.4
|
|
220
|
+
### 4.4 Unaugmented vs Augmented TypeRegistry
|
|
221
|
+
|
|
222
|
+
| Feature | Unaugmented `TypeRegistry` | Augmented `TypeRegistry` |
|
|
223
|
+
|---------|---------------------------|--------------------------|
|
|
224
|
+
| Constructor retrieval | `lookup('Name')` returns `TypeClass \| undefined` | `lookup('Name')` returns typed constructor |
|
|
225
|
+
| Type safety | Runtime only | Compile-time + runtime |
|
|
226
|
+
| Registry default | Empty interface | Per-key constructor signatures |
|
|
227
|
+
| Instance properties | `any` / `unknown` | Fully typed |
|
|
228
|
+
|
|
229
|
+
Two ways to augment: hand-written `.d.ts` (small projects, learning) or `@mnemonica/tactica` (auto-generated, recommended for non-trivial projects).
|
|
230
|
+
|
|
231
|
+
### 4.5 Why an Empty `TypeRegistry`?
|
|
214
232
|
|
|
215
233
|
The base `TypeRegistry` is intentionally empty:
|
|
216
234
|
|
|
@@ -457,6 +475,31 @@ const pageData = new routeData.PageData({ header: {...}, content: '' });
|
|
|
457
475
|
|
|
458
476
|
## 9. Common Mistakes
|
|
459
477
|
|
|
478
|
+
Quick reference before the details:
|
|
479
|
+
|
|
480
|
+
| Situation | Wrong | Right |
|
|
481
|
+
|---|---|---|
|
|
482
|
+
| Get a constructor | `import { T } from './collections/T.js'` | `const T = lookup('T')` |
|
|
483
|
+
| Create instance | `new T({ ... }) as unknown as TT` | `new T({ ... })` |
|
|
484
|
+
| Chain to child | `new (instance as any).Child({ ... })` | `new instance.Child({ ... })` |
|
|
485
|
+
| Type not found | Add `as any` | Augment `TypeRegistry` (manually or run `tactica`) |
|
|
486
|
+
|
|
487
|
+
### 9.0 When to Insist on What
|
|
488
|
+
|
|
489
|
+
When asked to work with a mnemonica project:
|
|
490
|
+
|
|
491
|
+
1. **Check whether `TypeRegistry` is augmented somewhere.** Look for:
|
|
492
|
+
- A `.tactica/` directory with `registry.ts` (tactica-generated), OR
|
|
493
|
+
- A `.d.ts` or `.ts` file containing `declare module 'mnemonica' { interface TypeRegistry { ... } }` (hand-written)
|
|
494
|
+
|
|
495
|
+
If neither exists, ask the user before writing code: do they want tactica set up, or to hand-augment?
|
|
496
|
+
|
|
497
|
+
2. **If `.tactica/` exists, check `tsconfig.json` includes it.** If missing, add `.tactica/**/*.ts` to `include`.
|
|
498
|
+
|
|
499
|
+
3. **Check that `lookup` is being used.** Direct imports from collection files + casts are the smell that augmentation is missing or unused.
|
|
500
|
+
|
|
501
|
+
4. **Never accept `as unknown as` as a solution.** It is a symptom of the augmentation being missing, not a fix.
|
|
502
|
+
|
|
460
503
|
### 9.1 "I'll just cast it"
|
|
461
504
|
|
|
462
505
|
```typescript
|
package/docs/tactica-pattern.md
CHANGED
|
@@ -143,5 +143,4 @@ Pick whichever fits your project. Both are first-class.
|
|
|
143
143
|
## Learn more
|
|
144
144
|
|
|
145
145
|
- [`./typed-lookup.md`](./typed-lookup.md) — `lookup` with or without tactica (canonical side-by-side reference)
|
|
146
|
-
- [
|
|
147
|
-
- [`../.ai/TACTICA-DEEP-DIVE.md`](../.ai/TACTICA-DEEP-DIVE.md) — comprehensive technical guide, declaration-merging deep dive
|
|
146
|
+
- [`./tactica-deep-dive.md`](./tactica-deep-dive.md) — comprehensive technical guide, declaration-merging deep dive, anti-patterns and "when to insist on what"
|
package/docs/typed-lookup.md
CHANGED
|
@@ -1,24 +1,28 @@
|
|
|
1
|
-
# Type-safe lookup with or without `@mnemonica/tactica`
|
|
1
|
+
# Type-safe `lookup()` and `define()` — with or without `@mnemonica/tactica`
|
|
2
2
|
|
|
3
|
-
`mnemonica`
|
|
3
|
+
`mnemonica` has exactly **one runtime**. Every `define()` call registers into the
|
|
4
|
+
same type graph, and every `lookup()` resolves against it, no matter which API
|
|
5
|
+
style you use.
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
TypeScript, however, cannot see a type graph built by runtime calls. There are
|
|
8
|
+
**three ways** to hand the registry to the compiler, and they compose:
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
| Approach | Needs `TypeRegistry` augmentation | Works with `@decorate()` | Instance subtypes typed | Best for |
|
|
10
|
+
| Approach | Extra tooling | `new instance.SubType()` typed | Free `lookup()` typed | `@decorate()` typed |
|
|
11
11
|
|---|---|---|---|---|
|
|
12
|
-
| Builder mode (
|
|
13
|
-
|
|
|
12
|
+
| **Builder mode** (default) | **None** | Yes | No (unless bridged) | No |
|
|
13
|
+
| **Registry bridge** (`RegistryOf`) | One hand-written line | Yes | **Yes** | No |
|
|
14
|
+
| **Tactica** (codegen) | `npx tactica` build step | Yes | Yes | **Yes** |
|
|
14
15
|
|
|
15
|
-
> **Rule of thumb:**
|
|
16
|
+
> **Rule of thumb:** start with builder mode — zero dependencies, zero codegen,
|
|
17
|
+
> types work out of the box. Add the one-line bridge the moment you want free
|
|
18
|
+
> `lookup()` typed. Reach for Tactica when you need `@decorate()`, or when a
|
|
19
|
+
> large codebase already uses free `define()` calls everywhere.
|
|
16
20
|
|
|
17
21
|
---
|
|
18
22
|
|
|
19
|
-
## Builder mode (
|
|
23
|
+
## Builder mode (default)
|
|
20
24
|
|
|
21
|
-
Import the module object or create a custom collection, then chain `.define()` calls. The returned object carries a local type registry.
|
|
25
|
+
Import the module object or create a custom collection, then chain `.define()` or `.lazy()` calls. The returned object carries a local type registry.
|
|
22
26
|
|
|
23
27
|
```typescript
|
|
24
28
|
import { mnemonica } from 'mnemonica';
|
|
@@ -68,15 +72,23 @@ const user = new User({ name: 'Ada' });
|
|
|
68
72
|
const admin = new user.Admin({ role: 'root' });
|
|
69
73
|
```
|
|
70
74
|
|
|
71
|
-
###
|
|
75
|
+
### Lookup resolution on a constructor
|
|
72
76
|
|
|
73
|
-
A constructor's own `lookup()`
|
|
77
|
+
A constructor's own `lookup()` resolves in two steps: **relative first, then
|
|
78
|
+
root fallback**. It searches the type's own subtypes (a relative path), and if
|
|
79
|
+
nothing matches it resolves the same string as an absolute path from the
|
|
80
|
+
collection root. These are all equivalent:
|
|
74
81
|
|
|
75
82
|
```typescript
|
|
76
|
-
const AdminA = App.lookup('User.Admin');
|
|
77
|
-
const AdminB = App.lookup('User').lookup('Admin');
|
|
83
|
+
const AdminA = App.lookup('User.Admin'); // absolute, via root fallback
|
|
84
|
+
const AdminB = App.lookup('User').lookup('Admin'); // root, then relative
|
|
85
|
+
const AdminC = App.lookup('Admin'); // works only if App has a descendant named Admin
|
|
78
86
|
```
|
|
79
87
|
|
|
88
|
+
Relative resolution wins when both could match: if a type has its own subtype
|
|
89
|
+
named `Admin`, `lookup('Admin')` returns that subtype, never a different
|
|
90
|
+
root-level `Admin`.
|
|
91
|
+
|
|
80
92
|
You can also look up a root constructor and call `.define()` on it directly:
|
|
81
93
|
|
|
82
94
|
```typescript
|
|
@@ -87,15 +99,15 @@ User.define('Guest', function (this: GuestShape, data: { token: string }) {
|
|
|
87
99
|
});
|
|
88
100
|
```
|
|
89
101
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
The imported `lookup()` function resolves against the **global** `TypeRegistry`, not against a builder object's local registry.
|
|
102
|
+
You can also use `.lazy()` when the constructor must be resolved through a
|
|
103
|
+
getter (for example, to break a circular dependency):
|
|
93
104
|
|
|
94
105
|
```typescript
|
|
95
|
-
|
|
106
|
+
const User = App.lookup('User');
|
|
96
107
|
|
|
97
|
-
const
|
|
98
|
-
|
|
108
|
+
const Guest = User.lazy('Guest', () => function (this: GuestShape, data: { token: string }) {
|
|
109
|
+
this.token = data.token;
|
|
110
|
+
});
|
|
99
111
|
```
|
|
100
112
|
|
|
101
113
|
### Constructing subtypes
|
|
@@ -108,15 +120,128 @@ const user = new User({ name: 'Ada' });
|
|
|
108
120
|
const admin = new user.Admin({ role: 'root' });
|
|
109
121
|
|
|
110
122
|
// WRONG at runtime
|
|
111
|
-
const Admin = App.lookup('Admin');
|
|
123
|
+
const Admin = App.lookup('User.Admin');
|
|
112
124
|
const admin2 = new Admin({ role: 'root' }); // fails: no parent instance
|
|
113
125
|
```
|
|
114
126
|
|
|
115
|
-
Direct `new` on a constructor returned by `.define()` works for **root types** only. For subtypes the parent instance is required. With `strictChain: true` (the default) the parent must be the immediate type; with `strictChain: false` ancestor instances further up the chain are also accepted.
|
|
127
|
+
Direct `new` on a constructor returned by `.define()` works for **root types** only. For subtypes the parent instance is required (or use `apply(parentInstance, Ctor, args)`). With `strictChain: true` (the default) the parent must be the immediate type; with `strictChain: false` ancestor instances further up the chain are also accepted.
|
|
116
128
|
|
|
117
129
|
---
|
|
118
130
|
|
|
119
|
-
##
|
|
131
|
+
## Multi-file usage: registry threading
|
|
132
|
+
|
|
133
|
+
**Yes — an exported builder value works across files.** Two reasons:
|
|
134
|
+
|
|
135
|
+
- **Runtime:** every chain registers into the same collection singleton
|
|
136
|
+
(`defaultTypes`, or the one `createTypesCollection()` you share), so imports
|
|
137
|
+
are irrelevant to what exists at runtime.
|
|
138
|
+
- **Compile time:** the registry rides in the *value's type*
|
|
139
|
+
(`IDefinitorInstance<..., Registry, Path>`), and values cross files freely
|
|
140
|
+
through `export` / `import`.
|
|
141
|
+
|
|
142
|
+
Split the chain through exports:
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
// models/user.ts
|
|
146
|
+
import { mnemonica } from 'mnemonica';
|
|
147
|
+
export const User = mnemonica.define('User', function (this: UserShape, data: { name: string }) {
|
|
148
|
+
this.name = data.name;
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// models/admin.ts
|
|
152
|
+
import { User } from './user';
|
|
153
|
+
export const Admin = User.define('Admin', function (this: AdminShape, data: { role: string }) {
|
|
154
|
+
this.role = data.role;
|
|
155
|
+
});
|
|
156
|
+
// Admin's type carries the accumulated registry: 'User' + 'User.Admin'
|
|
157
|
+
|
|
158
|
+
// anywhere.ts
|
|
159
|
+
import { Admin } from './models/admin';
|
|
160
|
+
const AdminCtor = Admin.lookup('User.Admin'); // typed
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Rule: thread the chain through exports and keep imports tree-shaped (no
|
|
164
|
+
cycles). Each file sees exactly the types registered up to its point in the
|
|
165
|
+
chain.
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## The registry bridge: `RegistryOf`
|
|
170
|
+
|
|
171
|
+
The builder registry is local to the value that carries it. If you also want
|
|
172
|
+
the **free** `lookup()` typed — without importing the builder everywhere — merge
|
|
173
|
+
the local registry into the global `TypeRegistry` with one hand-written line:
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
// registry.ts
|
|
177
|
+
import type { RegistryOf } from 'mnemonica';
|
|
178
|
+
import type { App } from './models';
|
|
179
|
+
|
|
180
|
+
declare module 'mnemonica' {
|
|
181
|
+
interface TypeRegistry extends RegistryOf<typeof App> {}
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
// anywhere.ts — import './registry' once (side effect)
|
|
187
|
+
import './registry';
|
|
188
|
+
import { lookup } from 'mnemonica';
|
|
189
|
+
|
|
190
|
+
const Admin = lookup('User.Admin'); // fully typed, no Tactica
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Why prefer this over hand-written augmentation (below): **single source of
|
|
194
|
+
truth**. The builder chain is the registry; the interface just re-exports it
|
|
195
|
+
into the global slot. Rename or restructure a type in the chain and the bridge
|
|
196
|
+
follows automatically — no second file to keep in sync, no codegen step.
|
|
197
|
+
|
|
198
|
+
Caveats:
|
|
199
|
+
|
|
200
|
+
- Keep the bridge in its own module (`registry.ts`). If a file both defines
|
|
201
|
+
the chain and uses free `lookup()`, import order decides whether the
|
|
202
|
+
augmentation is visible — a dedicated module avoids the question.
|
|
203
|
+
- The bridge cannot type `@decorate()` classes. A class decorator's return
|
|
204
|
+
type never changes the class binding's declared type (TypeScript limitation,
|
|
205
|
+
not mnemonica's), so decorated classes have no local registry to extract —
|
|
206
|
+
that is what Tactica is for.
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
## Explicit source: `lookup(source, path)` and `define(source, name, handler)`
|
|
211
|
+
|
|
212
|
+
The free `lookup()` resolves against the global `TypeRegistry`. The free
|
|
213
|
+
`define()` resolves against the default collection. When you hold a builder
|
|
214
|
+
value and want the free-function style anyway, pass it as the first argument —
|
|
215
|
+
the registry is then inferred from the source, not from the global interface:
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
import { lookup, define, lazy } from 'mnemonica';
|
|
219
|
+
|
|
220
|
+
// typed from App's registry, never a silent `TypeClass | undefined`
|
|
221
|
+
const Admin = lookup(App, 'User.Admin');
|
|
222
|
+
|
|
223
|
+
// same semantics as User.define('Guest', ...) — a subtype of User
|
|
224
|
+
const Guest = define(User, 'Guest', function (this: GuestShape, data: { token: string }) {
|
|
225
|
+
this.token = data.token;
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// same semantics as collection.define('NewRoot', ...) — a root type
|
|
229
|
+
const NewRoot = define(AppCollection, 'NewRoot', function (this: NewRootShape) { /* ... */ });
|
|
230
|
+
|
|
231
|
+
// lazy variants infer the registry from the source as well
|
|
232
|
+
const LazyGuest = lazy(User, 'Guest', () => function (this: GuestShape, data: { token: string }) {
|
|
233
|
+
this.token = data.token;
|
|
234
|
+
});
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Semantics follow the source object: a **collection** source defines a root
|
|
238
|
+
type, a **constructor** source defines a subtype. `lookup(source, path)`
|
|
239
|
+
resolves exactly like `source.lookup(path)` — relative first, root fallback.
|
|
240
|
+
The same source inference applies to `lazy(source, name?, getter, config?)`.
|
|
241
|
+
|
|
242
|
+
---
|
|
243
|
+
|
|
244
|
+
## Augmented mode (Tactica): for `@decorate()` and free-`define` codebases
|
|
120
245
|
|
|
121
246
|
If you use Tactica, you can write plain free functions and class decorators. Tactica scans your code and generates a `TypeRegistry` augmentation file.
|
|
122
247
|
|
|
@@ -147,6 +272,10 @@ const admin = new Admin({ role: 'root' });
|
|
|
147
272
|
|
|
148
273
|
### With `@decorate()`
|
|
149
274
|
|
|
275
|
+
> The canonical decorator guide is [`docs/decorate.md`](./decorate.md) —
|
|
276
|
+
> setup, examples, and the TypeØmatica integration live there. The short
|
|
277
|
+
> version:
|
|
278
|
+
|
|
150
279
|
```typescript
|
|
151
280
|
import { decorate } from 'mnemonica';
|
|
152
281
|
|
|
@@ -169,13 +298,18 @@ class Admin {
|
|
|
169
298
|
const AdminCtor = lookup('User.Admin');
|
|
170
299
|
```
|
|
171
300
|
|
|
172
|
-
> **Note:** `@decorate()`
|
|
301
|
+
> **Note:** `@decorate()` requires `TypeRegistry` augmentation (Tactica or
|
|
302
|
+
> hand-written). The builder API and the bridge cannot cover decorators,
|
|
303
|
+
> because TypeScript never applies a class decorator's return type to the
|
|
304
|
+
> class binding — there is no local registry to carry or extract.
|
|
173
305
|
|
|
174
306
|
---
|
|
175
307
|
|
|
176
|
-
##
|
|
308
|
+
## Hand-written `TypeRegistry` augmentation
|
|
177
309
|
|
|
178
|
-
If you are not using Tactica
|
|
310
|
+
If you are not using Tactica and the bridge does not fit (for example, types
|
|
311
|
+
defined only through free `define()` calls), you can write the augmentation
|
|
312
|
+
yourself.
|
|
179
313
|
|
|
180
314
|
```typescript
|
|
181
315
|
import type { TypeConstructor } from 'mnemonica';
|
|
@@ -188,25 +322,26 @@ declare module 'mnemonica' {
|
|
|
188
322
|
}
|
|
189
323
|
```
|
|
190
324
|
|
|
191
|
-
Once augmented, `lookup('User')` and `lookup('User.Admin')` resolve against `TypeRegistry` instead of falling back to `TypeClass | undefined`.
|
|
325
|
+
Once augmented, `lookup('User')` and `lookup('User.Admin')` resolve against `TypeRegistry` instead of falling back to `TypeClass | undefined`. For builder projects the `RegistryOf` bridge above is the same thing with one line and no duplication.
|
|
192
326
|
|
|
193
327
|
---
|
|
194
328
|
|
|
195
329
|
## Which type system wins when both are present?
|
|
196
330
|
|
|
197
|
-
Adding Tactica to a project that already uses the builder API does **not** replace the builder's local type system. The
|
|
331
|
+
Adding Tactica (or a bridge) to a project that already uses the builder API does **not** replace the builder's local type system. The systems are orthogonal and apply to different APIs.
|
|
198
332
|
|
|
199
333
|
| Call you write | Type system used | Why |
|
|
200
334
|
|---|---|---|
|
|
201
335
|
| `const App = mnemonica.define(...)` | Builder registry | The returned object carries its own type map. |
|
|
202
336
|
| `App.lookup('User')` | Builder registry | `lookup()` is a method on the builder object. |
|
|
337
|
+
| `lookup(App, 'User')` | Builder registry | The source argument carries the registry. |
|
|
203
338
|
| `new user.Admin(...)` | Builder registry | Subtypes are resolved from the instance's inferred chain type. |
|
|
204
339
|
| `define('User', ...)` free import | `TypeRegistry` augmentation | The free export has no local registry, so it relies on the global interface. |
|
|
205
|
-
| `lookup('User')` free import | `TypeRegistry` augmentation | Same as above. |
|
|
340
|
+
| `lookup('User')` free import | `TypeRegistry` augmentation | Same as above — populated by Tactica, hand-written, or the `RegistryOf` bridge. |
|
|
206
341
|
|
|
207
342
|
So if you keep using `mnemonica.define(...)` after installing Tactica, **the builder types stay in control**. Tactica only becomes relevant when you switch to the free `define` / `lookup` imports or use `@decorate()`.
|
|
208
343
|
|
|
209
|
-
At runtime the results are identical. At compile time, TypeScript picks whichever type system matches the exact identifier you used. If
|
|
344
|
+
At runtime the results are identical. At compile time, TypeScript picks whichever type system matches the exact identifier you used. If two sources disagree on the same path, the mismatch surfaces only on the API that resolves against `TypeRegistry`.
|
|
210
345
|
|
|
211
346
|
---
|
|
212
347
|
|
|
@@ -236,7 +371,7 @@ declare module 'mnemonica' {
|
|
|
236
371
|
}
|
|
237
372
|
```
|
|
238
373
|
|
|
239
|
-
Function bodies, return types, and generic parameters cannot produce declarations. That is why Tactica generates them: it is a build-time tool that pretends to be a programmer who wrote all the augmentation blocks by hand.
|
|
374
|
+
Function bodies, return types, and generic parameters cannot produce declarations. That is why Tactica generates them: it is a build-time tool that pretends to be a programmer who wrote all the augmentation blocks by hand. The `RegistryOf` bridge is the same declaration, written once, with its contents *computed* from the builder value instead of repeated by hand.
|
|
240
375
|
|
|
241
376
|
### What the builder mode can and cannot do
|
|
242
377
|
|
|
@@ -244,16 +379,14 @@ The builder mode solves the problem locally by carrying the registry in the retu
|
|
|
244
379
|
|
|
245
380
|
```typescript
|
|
246
381
|
const App = mnemonica.define('User', ...).define('Admin', ...);
|
|
247
|
-
// App's type is roughly { lookup: { User: ..., Admin: ... } }
|
|
382
|
+
// App's type is roughly { lookup: { User: ..., 'User.Admin': ... } }
|
|
248
383
|
```
|
|
249
384
|
|
|
250
|
-
This works because the function's return type can be computed from the generic parameters. The registry is local,
|
|
385
|
+
This works because the function's return type can be computed from the generic parameters. The registry is local to the value — but the value can be exported, so the registry travels across files (registry threading), and it can be merged into the global interface (the `RegistryOf` bridge).
|
|
251
386
|
|
|
252
|
-
|
|
387
|
+
What it still cannot handle:
|
|
253
388
|
|
|
254
|
-
-
|
|
255
|
-
- **`@decorate()` classes** — decorators are independent declarations. They cannot chain on a shared object.
|
|
256
|
-
- **Cross-file type references** — the builder registry is tied to the variable where the chain was built.
|
|
389
|
+
- **`@decorate()` classes** — a class decorator's return type never changes the class binding's declared type, so there is no channel for a local registry at all.
|
|
257
390
|
|
|
258
391
|
### What the augmented mode can and cannot do
|
|
259
392
|
|
|
@@ -263,27 +396,27 @@ The augmented mode uses the global `TypeRegistry` interface. It can handle:
|
|
|
263
396
|
- `@decorate()` classes.
|
|
264
397
|
- Cross-file references because the interface is globally visible.
|
|
265
398
|
|
|
266
|
-
But it requires external tooling (Tactica) or hand-written
|
|
399
|
+
But it requires external tooling (Tactica) or a hand-written declaration (directly, or via the `RegistryOf` bridge). Without that, the interface is empty and free `lookup()` falls back to `TypeClass | undefined`.
|
|
267
400
|
|
|
268
401
|
### Why not unify them?
|
|
269
402
|
|
|
270
|
-
The
|
|
403
|
+
The split is a consequence of TypeScript's design, not mnemonica's. You cannot have one mechanism that covers all three cases:
|
|
271
404
|
|
|
272
405
|
1. Chained builder calls.
|
|
273
406
|
2. Free function calls.
|
|
274
407
|
3. Decorated classes.
|
|
275
408
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
The runtime is the same. The difference is only in how TypeScript discovers the types.
|
|
409
|
+
So mnemonica exposes the builder API for case 1, the bridge and two-arg
|
|
410
|
+
overloads to connect cases 1 and 2, and the augmented API for cases 2 and 3.
|
|
411
|
+
The runtime is the same everywhere; only the way TypeScript discovers the
|
|
412
|
+
types differs.
|
|
282
413
|
|
|
283
414
|
---
|
|
284
415
|
|
|
285
416
|
## Summary
|
|
286
417
|
|
|
287
|
-
- **
|
|
288
|
-
- **
|
|
289
|
-
- **
|
|
418
|
+
- **Default:** builder mode — `mnemonica.define(...)` or `createTypesCollection()`, `.lookup()` on the result. No tooling.
|
|
419
|
+
- **Multiple files:** thread the chain through exports; the registry type travels with the value.
|
|
420
|
+
- **Want free `lookup()` typed:** add the one-line `RegistryOf` bridge in a `registry.ts`.
|
|
421
|
+
- **Mixing styles:** `lookup(source, path)` / `define(source, name, handler)` infer the registry from the source value.
|
|
422
|
+
- **Need `@decorate()` typed:** use Tactica — it is the only path, and the reason is a TypeScript limitation, not a missing feature.
|