mnemonica 1.0.9 → 1.2.0
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 +23 -6
- package/.ai/PROTOTYPE-CHAIN.md +21 -0
- 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} +66 -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/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 +96 -24
- package/FOR_HUMANS.md +32 -76
- package/README.md +35 -20
- package/SKILL.md +20 -20
- package/build/api/index.d.ts +2 -2
- package/build/api/types/index.js +8 -3
- package/build/descriptors/types/index.js +2 -2
- package/build/index.d.ts +10 -3
- package/build/index.js +17 -5
- package/build/types/index.d.ts +60 -21
- package/build/types/index.js +1 -1
- package/docs/ai-learning-trajectory.md +2 -3
- package/docs/decorate.md +123 -0
- package/docs/hott-primer.md +1 -1
- package/docs/purpose.md +3 -3
- package/{.ai/TACTICA-DEEP-DIVE.md → docs/tactica-deep-dive.md} +45 -2
- package/docs/tactica-pattern.md +1 -2
- package/docs/typed-lookup.md +337 -93
- package/package.json +6 -6
- package/src/api/types/index.ts +14 -4
- package/src/descriptors/types/index.ts +63 -58
- package/src/index.ts +103 -12
- package/src/types/index.ts +241 -41
- 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/define-patterns.md → rules-define-patterns.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
package/docs/typed-lookup.md
CHANGED
|
@@ -1,161 +1,405 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Type-safe `lookup()` and `define()` — with or without `@mnemonica/tactica`
|
|
2
2
|
|
|
3
|
-
`
|
|
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.
|
|
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:
|
|
9
|
+
|
|
10
|
+
| Approach | Extra tooling | `new instance.SubType()` typed | Free `lookup()` typed | `@decorate()` typed |
|
|
11
|
+
|---|---|---|---|---|
|
|
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** |
|
|
15
|
+
|
|
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.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Builder mode (default)
|
|
24
|
+
|
|
25
|
+
Import the module object or create a custom collection, then chain `.define()` calls. The returned object carries a local type registry.
|
|
4
26
|
|
|
5
27
|
```typescript
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
28
|
+
import { mnemonica } from 'mnemonica';
|
|
29
|
+
|
|
30
|
+
interface UserShape {
|
|
31
|
+
name: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface AdminShape extends UserShape {
|
|
35
|
+
role: string;
|
|
10
36
|
}
|
|
11
37
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
)
|
|
38
|
+
const App = mnemonica
|
|
39
|
+
.define('User', function (this: UserShape, data: { name: string }) {
|
|
40
|
+
this.name = data.name;
|
|
41
|
+
})
|
|
42
|
+
// Names passed to a constructor's `.define()` are relative to that constructor.
|
|
43
|
+
.define('Admin', function (this: AdminShape, data: { role: string }) {
|
|
44
|
+
this.role = data.role;
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// `App.lookup('User')` is typed from the builder registry.
|
|
48
|
+
const User = App.lookup('User');
|
|
49
|
+
const user = new User({ name: 'Ada' });
|
|
50
|
+
|
|
51
|
+
// Subtypes are constructed from a parent instance.
|
|
52
|
+
const admin = new user.Admin({ role: 'root' });
|
|
20
53
|
```
|
|
21
54
|
|
|
22
|
-
|
|
55
|
+
### Custom collections
|
|
23
56
|
|
|
24
|
-
|
|
57
|
+
`createTypesCollection()` works the same way and is fully isolated from the default collection.
|
|
25
58
|
|
|
26
|
-
|
|
59
|
+
```typescript
|
|
60
|
+
import { createTypesCollection } from 'mnemonica';
|
|
61
|
+
|
|
62
|
+
const AppCollection = createTypesCollection()
|
|
63
|
+
.define('User', function (this: UserShape, data: { name: string }) {
|
|
64
|
+
this.name = data.name;
|
|
65
|
+
})
|
|
66
|
+
.define('Admin', function (this: AdminShape, data: { role: string }) {
|
|
67
|
+
this.role = data.role;
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const User = AppCollection.lookup('User');
|
|
71
|
+
const user = new User({ name: 'Ada' });
|
|
72
|
+
const admin = new user.Admin({ role: 'root' });
|
|
73
|
+
```
|
|
27
74
|
|
|
28
|
-
|
|
75
|
+
### Lookup resolution on a constructor
|
|
29
76
|
|
|
30
|
-
|
|
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:
|
|
31
81
|
|
|
32
82
|
```typescript
|
|
33
|
-
//
|
|
34
|
-
|
|
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
|
|
86
|
+
```
|
|
35
87
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
+
|
|
92
|
+
You can also look up a root constructor and call `.define()` on it directly:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
const User = App.lookup('User');
|
|
39
96
|
|
|
40
|
-
|
|
41
|
-
|
|
97
|
+
User.define('Guest', function (this: GuestShape, data: { token: string }) {
|
|
98
|
+
this.token = data.token;
|
|
42
99
|
});
|
|
43
100
|
```
|
|
44
101
|
|
|
45
|
-
|
|
102
|
+
### Constructing subtypes
|
|
103
|
+
|
|
104
|
+
Subtypes must be created from a parent instance. Do not call `new` on a constructor returned by `lookup()` for a subtype.
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
// OK
|
|
108
|
+
const user = new User({ name: 'Ada' });
|
|
109
|
+
const admin = new user.Admin({ role: 'root' });
|
|
110
|
+
|
|
111
|
+
// WRONG at runtime
|
|
112
|
+
const Admin = App.lookup('User.Admin');
|
|
113
|
+
const admin2 = new Admin({ role: 'root' }); // fails: no parent instance
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
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.
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## Multi-file usage: registry threading
|
|
121
|
+
|
|
122
|
+
**Yes — an exported builder value works across files.** Two reasons:
|
|
123
|
+
|
|
124
|
+
- **Runtime:** every chain registers into the same collection singleton
|
|
125
|
+
(`defaultTypes`, or the one `createTypesCollection()` you share), so imports
|
|
126
|
+
are irrelevant to what exists at runtime.
|
|
127
|
+
- **Compile time:** the registry rides in the *value's type*
|
|
128
|
+
(`IDefinitorInstance<..., Registry, Path>`), and values cross files freely
|
|
129
|
+
through `export` / `import`.
|
|
130
|
+
|
|
131
|
+
Split the chain through exports:
|
|
46
132
|
|
|
47
133
|
```typescript
|
|
48
|
-
//
|
|
49
|
-
import
|
|
134
|
+
// models/user.ts
|
|
135
|
+
import { mnemonica } from 'mnemonica';
|
|
136
|
+
export const User = mnemonica.define('User', function (this: UserShape, data: { name: string }) {
|
|
137
|
+
this.name = data.name;
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// models/admin.ts
|
|
141
|
+
import { User } from './user';
|
|
142
|
+
export const Admin = User.define('Admin', function (this: AdminShape, data: { role: string }) {
|
|
143
|
+
this.role = data.role;
|
|
144
|
+
});
|
|
145
|
+
// Admin's type carries the accumulated registry: 'User' + 'User.Admin'
|
|
146
|
+
|
|
147
|
+
// anywhere.ts
|
|
148
|
+
import { Admin } from './models/admin';
|
|
149
|
+
const AdminCtor = Admin.lookup('User.Admin'); // typed
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Rule: thread the chain through exports and keep imports tree-shaped (no
|
|
153
|
+
cycles). Each file sees exactly the types registered up to its point in the
|
|
154
|
+
chain.
|
|
155
|
+
|
|
156
|
+
---
|
|
50
157
|
|
|
51
|
-
|
|
52
|
-
export type PersonInstance = {
|
|
53
|
-
name : string;
|
|
54
|
-
// Subtypes available on instances of this type:
|
|
55
|
-
Employee : new (data: { role: string }) => EmployeeInstance;
|
|
56
|
-
};
|
|
158
|
+
## The registry bridge: `RegistryOf`
|
|
57
159
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
160
|
+
The builder registry is local to the value that carries it. If you also want
|
|
161
|
+
the **free** `lookup()` typed — without importing the builder everywhere — merge
|
|
162
|
+
the local registry into the global `TypeRegistry` with one hand-written line:
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
// registry.ts
|
|
166
|
+
import type { RegistryOf } from 'mnemonica';
|
|
167
|
+
import type { App } from './models';
|
|
62
168
|
|
|
63
169
|
declare module 'mnemonica' {
|
|
64
|
-
|
|
65
|
-
'Person' : new (data: { name: string }) => PersonInstance;
|
|
66
|
-
'Person.Employee' : new (data: { role: string }) => EmployeeInstance;
|
|
67
|
-
}
|
|
170
|
+
interface TypeRegistry extends RegistryOf<typeof App> {}
|
|
68
171
|
}
|
|
69
172
|
```
|
|
70
173
|
|
|
71
|
-
|
|
174
|
+
```typescript
|
|
175
|
+
// anywhere.ts — import './registry' once (side effect)
|
|
176
|
+
import './registry';
|
|
177
|
+
import { lookup } from 'mnemonica';
|
|
72
178
|
|
|
73
|
-
|
|
74
|
-
{
|
|
75
|
-
"include": ["src/**/*.ts", "types/**/*.d.ts"]
|
|
76
|
-
}
|
|
179
|
+
const Admin = lookup('User.Admin'); // fully typed, no Tactica
|
|
77
180
|
```
|
|
78
181
|
|
|
79
|
-
|
|
182
|
+
Why prefer this over hand-written augmentation (below): **single source of
|
|
183
|
+
truth**. The builder chain is the registry; the interface just re-exports it
|
|
184
|
+
into the global slot. Rename or restructure a type in the chain and the bridge
|
|
185
|
+
follows automatically — no second file to keep in sync, no codegen step.
|
|
186
|
+
|
|
187
|
+
Caveats:
|
|
188
|
+
|
|
189
|
+
- Keep the bridge in its own module (`registry.ts`). If a file both defines
|
|
190
|
+
the chain and uses free `lookup()`, import order decides whether the
|
|
191
|
+
augmentation is visible — a dedicated module avoids the question.
|
|
192
|
+
- The bridge cannot type `@decorate()` classes. A class decorator's return
|
|
193
|
+
type never changes the class binding's declared type (TypeScript limitation,
|
|
194
|
+
not mnemonica's), so decorated classes have no local registry to extract —
|
|
195
|
+
that is what Tactica is for.
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
## Explicit source: `lookup(source, path)` and `define(source, name, handler)`
|
|
200
|
+
|
|
201
|
+
The free `lookup()` resolves against the global `TypeRegistry`. The free
|
|
202
|
+
`define()` resolves against the default collection. When you hold a builder
|
|
203
|
+
value and want the free-function style anyway, pass it as the first argument —
|
|
204
|
+
the registry is then inferred from the source, not from the global interface:
|
|
80
205
|
|
|
81
206
|
```typescript
|
|
82
|
-
import { lookup } from 'mnemonica';
|
|
207
|
+
import { lookup, define } from 'mnemonica';
|
|
208
|
+
|
|
209
|
+
// typed from App's registry, never a silent `TypeClass | undefined`
|
|
210
|
+
const Admin = lookup(App, 'User.Admin');
|
|
83
211
|
|
|
84
|
-
|
|
85
|
-
const
|
|
212
|
+
// same semantics as User.define('Guest', ...) — a subtype of User
|
|
213
|
+
const Guest = define(User, 'Guest', function (this: GuestShape, data: { token: string }) {
|
|
214
|
+
this.token = data.token;
|
|
215
|
+
});
|
|
86
216
|
|
|
87
|
-
|
|
88
|
-
const
|
|
217
|
+
// same semantics as collection.define('NewRoot', ...) — a root type
|
|
218
|
+
const NewRoot = define(AppCollection, 'NewRoot', function (this: NewRootShape) { /* ... */ });
|
|
89
219
|
```
|
|
90
220
|
|
|
91
|
-
|
|
221
|
+
Semantics follow the source object: a **collection** source defines a root
|
|
222
|
+
type, a **constructor** source defines a subtype. `lookup(source, path)`
|
|
223
|
+
resolves exactly like `source.lookup(path)` — relative first, root fallback.
|
|
92
224
|
|
|
93
225
|
---
|
|
94
226
|
|
|
95
|
-
##
|
|
227
|
+
## Augmented mode (Tactica): for `@decorate()` and free-`define` codebases
|
|
96
228
|
|
|
97
|
-
|
|
229
|
+
If you use Tactica, you can write plain free functions and class decorators. Tactica scans your code and generates a `TypeRegistry` augmentation file.
|
|
98
230
|
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
|
|
231
|
+
```typescript
|
|
232
|
+
import { define, lookup, decorate } from 'mnemonica';
|
|
233
|
+
|
|
234
|
+
interface UserShape {
|
|
235
|
+
name: string;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
interface AdminShape extends UserShape {
|
|
239
|
+
role: string;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Tactica will add these to TypeRegistry automatically.
|
|
243
|
+
define('User', function (this: UserShape, data: { name: string }) {
|
|
244
|
+
this.name = data.name;
|
|
245
|
+
});
|
|
102
246
|
|
|
103
|
-
|
|
247
|
+
define('User.Admin', function (this: AdminShape, data: { role: string }) {
|
|
248
|
+
this.role = data.role;
|
|
249
|
+
});
|
|
104
250
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
251
|
+
// Now the free lookup is typed.
|
|
252
|
+
const Admin = lookup('User.Admin');
|
|
253
|
+
const admin = new Admin({ role: 'root' });
|
|
108
254
|
```
|
|
109
255
|
|
|
110
|
-
|
|
256
|
+
### With `@decorate()`
|
|
111
257
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
258
|
+
> The canonical decorator guide is [`docs/decorate.md`](./decorate.md) —
|
|
259
|
+
> setup, examples, and the TypeØmatica integration live there. The short
|
|
260
|
+
> version:
|
|
261
|
+
|
|
262
|
+
```typescript
|
|
263
|
+
import { decorate } from 'mnemonica';
|
|
264
|
+
|
|
265
|
+
@decorate()
|
|
266
|
+
class User {
|
|
267
|
+
name: string;
|
|
268
|
+
constructor(data: { name: string }) {
|
|
269
|
+
this.name = data.name;
|
|
270
|
+
}
|
|
115
271
|
}
|
|
116
|
-
```
|
|
117
272
|
|
|
118
|
-
|
|
273
|
+
@decorate(User)
|
|
274
|
+
class Admin {
|
|
275
|
+
role: string;
|
|
276
|
+
constructor(data: { role: string }) {
|
|
277
|
+
this.role = data.role;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
119
280
|
|
|
120
|
-
|
|
281
|
+
const AdminCtor = lookup('User.Admin');
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
> **Note:** `@decorate()` requires `TypeRegistry` augmentation (Tactica or
|
|
285
|
+
> hand-written). The builder API and the bridge cannot cover decorators,
|
|
286
|
+
> because TypeScript never applies a class decorator's return type to the
|
|
287
|
+
> class binding — there is no local registry to carry or extract.
|
|
121
288
|
|
|
122
289
|
---
|
|
123
290
|
|
|
124
|
-
##
|
|
291
|
+
## Hand-written `TypeRegistry` augmentation
|
|
125
292
|
|
|
126
|
-
|
|
293
|
+
If you are not using Tactica and the bridge does not fit (for example, types
|
|
294
|
+
defined only through free `define()` calls), you can write the augmentation
|
|
295
|
+
yourself.
|
|
127
296
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
- Projects that want to keep the `TypeRegistry` surface stable and curated, independent of source-code churn
|
|
131
|
-
- Environments where running a CLI/codegen utility is inconvenient
|
|
297
|
+
```typescript
|
|
298
|
+
import type { TypeConstructor } from 'mnemonica';
|
|
132
299
|
|
|
133
|
-
|
|
300
|
+
declare module 'mnemonica' {
|
|
301
|
+
interface TypeRegistry {
|
|
302
|
+
'User': TypeConstructor<UserShape>;
|
|
303
|
+
'User.Admin': TypeConstructor<AdminShape>;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
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.
|
|
134
309
|
|
|
135
|
-
|
|
136
|
-
- You don't want to maintain the augmentation by hand as `define()` calls are added or refactored
|
|
137
|
-
- You want full instance-subtype typing (`alice.Employee`) without writing it
|
|
138
|
-
- You're already running tactica as part of your build or IDE workflow
|
|
310
|
+
---
|
|
139
311
|
|
|
140
|
-
|
|
312
|
+
## Which type system wins when both are present?
|
|
313
|
+
|
|
314
|
+
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.
|
|
315
|
+
|
|
316
|
+
| Call you write | Type system used | Why |
|
|
317
|
+
|---|---|---|
|
|
318
|
+
| `const App = mnemonica.define(...)` | Builder registry | The returned object carries its own type map. |
|
|
319
|
+
| `App.lookup('User')` | Builder registry | `lookup()` is a method on the builder object. |
|
|
320
|
+
| `lookup(App, 'User')` | Builder registry | The source argument carries the registry. |
|
|
321
|
+
| `new user.Admin(...)` | Builder registry | Subtypes are resolved from the instance's inferred chain type. |
|
|
322
|
+
| `define('User', ...)` free import | `TypeRegistry` augmentation | The free export has no local registry, so it relies on the global interface. |
|
|
323
|
+
| `lookup('User')` free import | `TypeRegistry` augmentation | Same as above — populated by Tactica, hand-written, or the `RegistryOf` bridge. |
|
|
324
|
+
|
|
325
|
+
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()`.
|
|
326
|
+
|
|
327
|
+
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`.
|
|
141
328
|
|
|
142
329
|
---
|
|
143
330
|
|
|
144
|
-
##
|
|
331
|
+
## Why two type systems exist
|
|
332
|
+
|
|
333
|
+
It feels like there should be one type system. In an ideal world, every `define()` call would add its type to the same registry, and `lookup()` would always be typed. TypeScript does not allow that.
|
|
334
|
+
|
|
335
|
+
### The hard limitation
|
|
145
336
|
|
|
146
|
-
|
|
337
|
+
TypeScript cannot extend a global interface from inside a function signature. This code is impossible in TypeScript:
|
|
147
338
|
|
|
148
339
|
```typescript
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
340
|
+
function define<N extends string, T>(name: N, handler: (this: T) => void) {
|
|
341
|
+
// There is no type-level statement that says:
|
|
342
|
+
// "Add N to the global TypeRegistry interface."
|
|
343
|
+
// The language simply does not have it.
|
|
344
|
+
}
|
|
152
345
|
```
|
|
153
346
|
|
|
347
|
+
Interfaces can only be extended by explicit declarations:
|
|
348
|
+
|
|
154
349
|
```typescript
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
350
|
+
declare module 'mnemonica' {
|
|
351
|
+
interface TypeRegistry {
|
|
352
|
+
'User': TypeConstructor<UserShape>;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
159
355
|
```
|
|
160
356
|
|
|
161
|
-
|
|
357
|
+
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.
|
|
358
|
+
|
|
359
|
+
### What the builder mode can and cannot do
|
|
360
|
+
|
|
361
|
+
The builder mode solves the problem locally by carrying the registry in the returned object:
|
|
362
|
+
|
|
363
|
+
```typescript
|
|
364
|
+
const App = mnemonica.define('User', ...).define('Admin', ...);
|
|
365
|
+
// App's type is roughly { lookup: { User: ..., 'User.Admin': ... } }
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
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).
|
|
369
|
+
|
|
370
|
+
What it still cannot handle:
|
|
371
|
+
|
|
372
|
+
- **`@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.
|
|
373
|
+
|
|
374
|
+
### What the augmented mode can and cannot do
|
|
375
|
+
|
|
376
|
+
The augmented mode uses the global `TypeRegistry` interface. It can handle:
|
|
377
|
+
|
|
378
|
+
- Free `define()` and `lookup()` anywhere in the project.
|
|
379
|
+
- `@decorate()` classes.
|
|
380
|
+
- Cross-file references because the interface is globally visible.
|
|
381
|
+
|
|
382
|
+
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`.
|
|
383
|
+
|
|
384
|
+
### Why not unify them?
|
|
385
|
+
|
|
386
|
+
The split is a consequence of TypeScript's design, not mnemonica's. You cannot have one mechanism that covers all three cases:
|
|
387
|
+
|
|
388
|
+
1. Chained builder calls.
|
|
389
|
+
2. Free function calls.
|
|
390
|
+
3. Decorated classes.
|
|
391
|
+
|
|
392
|
+
So mnemonica exposes the builder API for case 1, the bridge and two-arg
|
|
393
|
+
overloads to connect cases 1 and 2, and the augmented API for cases 2 and 3.
|
|
394
|
+
The runtime is the same everywhere; only the way TypeScript discovers the
|
|
395
|
+
types differs.
|
|
396
|
+
|
|
397
|
+
---
|
|
398
|
+
|
|
399
|
+
## Summary
|
|
400
|
+
|
|
401
|
+
- **Default:** builder mode — `mnemonica.define(...)` or `createTypesCollection()`, `.lookup()` on the result. No tooling.
|
|
402
|
+
- **Multiple files:** thread the chain through exports; the registry type travels with the value.
|
|
403
|
+
- **Want free `lookup()` typed:** add the one-line `RegistryOf` bridge in a `registry.ts`.
|
|
404
|
+
- **Mixing styles:** `lookup(source, path)` / `define(source, name, handler)` infer the registry from the source value.
|
|
405
|
+
- **Need `@decorate()` typed:** use Tactica — it is the only path, and the reason is a TypeScript limitation, not a missing feature.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mnemonica",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "AI-agent ready instance inheritance system for JavaScript/TypeScript via prototype chains",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./build/index.js",
|
|
@@ -95,15 +95,15 @@
|
|
|
95
95
|
},
|
|
96
96
|
"homepage": "https://github.com/wentout/mnemonica#readme",
|
|
97
97
|
"devDependencies": {
|
|
98
|
-
"@eslint/eslintrc": "^3.3.
|
|
98
|
+
"@eslint/eslintrc": "^3.3.6",
|
|
99
99
|
"@eslint/js": "^9.21.0",
|
|
100
100
|
"@mnemonica/tactica": "^0.1.3",
|
|
101
101
|
"@types/jest": "^29.5.14",
|
|
102
|
-
"@types/node": "^22.
|
|
103
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
104
|
-
"@typescript-eslint/parser": "^8.
|
|
102
|
+
"@types/node": "^22.20.1",
|
|
103
|
+
"@typescript-eslint/eslint-plugin": "^8.64.0",
|
|
104
|
+
"@typescript-eslint/parser": "^8.64.0",
|
|
105
105
|
"chai": "^5.3.3",
|
|
106
|
-
"eslint": "^9.39.
|
|
106
|
+
"eslint": "^9.39.5",
|
|
107
107
|
"eslint-plugin-import": "^2.32.0",
|
|
108
108
|
"eslint-plugin-mocha": "^10.5.0",
|
|
109
109
|
"eslint-plugin-no-arrow-this": "^1.2.0",
|
package/src/api/types/index.ts
CHANGED
|
@@ -212,11 +212,21 @@ TypeDescriptor.prototype.lookup = function (
|
|
|
212
212
|
this: TypeDescriptorInstance,
|
|
213
213
|
TypeNestedPath: string
|
|
214
214
|
) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
215
|
+
// relative first: resolve among this type's own subtypes,
|
|
216
|
+
// e.g. AdminType.lookup('SubType') or 'SubType.DeepSubType'
|
|
217
|
+
const relativeResult = lookup.call(
|
|
218
|
+
this.subtypes as TypesMap,
|
|
219
|
+
TypeNestedPath
|
|
218
220
|
);
|
|
219
|
-
|
|
221
|
+
if (relativeResult) {
|
|
222
|
+
return relativeResult;
|
|
223
|
+
}
|
|
224
|
+
// root fallback: resolve absolute registry paths from the
|
|
225
|
+
// collection, e.g. AdminType.lookup('UserType.AdminType'),
|
|
226
|
+
// which is what the TypeLookup<Registry> type contract promises
|
|
227
|
+
const collection = this.collection as CollectionDef;
|
|
228
|
+
const rootResult = collection.lookup(TypeNestedPath);
|
|
229
|
+
return rootResult;
|
|
220
230
|
};
|
|
221
231
|
|
|
222
232
|
odp(
|