mnemonica 1.1.0 → 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 +62 -31
- package/FOR_HUMANS.md +20 -72
- package/README.md +23 -21
- package/SKILL.md +20 -20
- package/build/api/types/index.js +8 -3
- package/build/index.d.ts +9 -2
- package/build/index.js +17 -5
- package/build/types/index.d.ts +2 -0
- 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} +44 -1
- package/docs/tactica-pattern.md +1 -2
- package/docs/typed-lookup.md +170 -54
- package/package.json +1 -1
- package/src/api/types/index.ts +14 -4
- package/src/index.ts +84 -7
- package/src/types/index.ts +15 -0
- 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/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/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/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,22 +1,26 @@
|
|
|
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
25
|
Import the module object or create a custom collection, then chain `.define()` calls. The returned object carries a local type registry.
|
|
22
26
|
|
|
@@ -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,17 +99,6 @@ User.define('Guest', function (this: GuestShape, data: { token: string }) {
|
|
|
87
99
|
});
|
|
88
100
|
```
|
|
89
101
|
|
|
90
|
-
### Do not mix free `lookup()` with the builder
|
|
91
|
-
|
|
92
|
-
The imported `lookup()` function resolves against the **global** `TypeRegistry`, not against a builder object's local registry.
|
|
93
|
-
|
|
94
|
-
```typescript
|
|
95
|
-
import { lookup } from 'mnemonica';
|
|
96
|
-
|
|
97
|
-
const UserA = App.lookup('User'); // typed from the builder registry
|
|
98
|
-
const UserB = lookup('User'); // TypeClass | undefined unless TypeRegistry is augmented
|
|
99
|
-
```
|
|
100
|
-
|
|
101
102
|
### Constructing subtypes
|
|
102
103
|
|
|
103
104
|
Subtypes must be created from a parent instance. Do not call `new` on a constructor returned by `lookup()` for a subtype.
|
|
@@ -108,15 +109,122 @@ const user = new User({ name: 'Ada' });
|
|
|
108
109
|
const admin = new user.Admin({ role: 'root' });
|
|
109
110
|
|
|
110
111
|
// WRONG at runtime
|
|
111
|
-
const Admin = App.lookup('Admin');
|
|
112
|
+
const Admin = App.lookup('User.Admin');
|
|
112
113
|
const admin2 = new Admin({ role: 'root' }); // fails: no parent instance
|
|
113
114
|
```
|
|
114
115
|
|
|
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.
|
|
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:
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
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
|
+
---
|
|
157
|
+
|
|
158
|
+
## The registry bridge: `RegistryOf`
|
|
159
|
+
|
|
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';
|
|
168
|
+
|
|
169
|
+
declare module 'mnemonica' {
|
|
170
|
+
interface TypeRegistry extends RegistryOf<typeof App> {}
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
// anywhere.ts — import './registry' once (side effect)
|
|
176
|
+
import './registry';
|
|
177
|
+
import { lookup } from 'mnemonica';
|
|
178
|
+
|
|
179
|
+
const Admin = lookup('User.Admin'); // fully typed, no Tactica
|
|
180
|
+
```
|
|
181
|
+
|
|
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:
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
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');
|
|
211
|
+
|
|
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
|
+
});
|
|
216
|
+
|
|
217
|
+
// same semantics as collection.define('NewRoot', ...) — a root type
|
|
218
|
+
const NewRoot = define(AppCollection, 'NewRoot', function (this: NewRootShape) { /* ... */ });
|
|
219
|
+
```
|
|
220
|
+
|
|
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.
|
|
116
224
|
|
|
117
225
|
---
|
|
118
226
|
|
|
119
|
-
## Augmented mode (
|
|
227
|
+
## Augmented mode (Tactica): for `@decorate()` and free-`define` codebases
|
|
120
228
|
|
|
121
229
|
If you use Tactica, you can write plain free functions and class decorators. Tactica scans your code and generates a `TypeRegistry` augmentation file.
|
|
122
230
|
|
|
@@ -147,6 +255,10 @@ const admin = new Admin({ role: 'root' });
|
|
|
147
255
|
|
|
148
256
|
### With `@decorate()`
|
|
149
257
|
|
|
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
|
+
|
|
150
262
|
```typescript
|
|
151
263
|
import { decorate } from 'mnemonica';
|
|
152
264
|
|
|
@@ -169,13 +281,18 @@ class Admin {
|
|
|
169
281
|
const AdminCtor = lookup('User.Admin');
|
|
170
282
|
```
|
|
171
283
|
|
|
172
|
-
> **Note:** `@decorate()`
|
|
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.
|
|
173
288
|
|
|
174
289
|
---
|
|
175
290
|
|
|
176
|
-
##
|
|
291
|
+
## Hand-written `TypeRegistry` augmentation
|
|
177
292
|
|
|
178
|
-
If you are not using Tactica
|
|
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.
|
|
179
296
|
|
|
180
297
|
```typescript
|
|
181
298
|
import type { TypeConstructor } from 'mnemonica';
|
|
@@ -188,25 +305,26 @@ declare module 'mnemonica' {
|
|
|
188
305
|
}
|
|
189
306
|
```
|
|
190
307
|
|
|
191
|
-
Once augmented, `lookup('User')` and `lookup('User.Admin')` resolve against `TypeRegistry` instead of falling back to `TypeClass | undefined`.
|
|
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.
|
|
192
309
|
|
|
193
310
|
---
|
|
194
311
|
|
|
195
312
|
## Which type system wins when both are present?
|
|
196
313
|
|
|
197
|
-
Adding Tactica to a project that already uses the builder API does **not** replace the builder's local type system. The
|
|
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.
|
|
198
315
|
|
|
199
316
|
| Call you write | Type system used | Why |
|
|
200
317
|
|---|---|---|
|
|
201
318
|
| `const App = mnemonica.define(...)` | Builder registry | The returned object carries its own type map. |
|
|
202
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. |
|
|
203
321
|
| `new user.Admin(...)` | Builder registry | Subtypes are resolved from the instance's inferred chain type. |
|
|
204
322
|
| `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. |
|
|
323
|
+
| `lookup('User')` free import | `TypeRegistry` augmentation | Same as above — populated by Tactica, hand-written, or the `RegistryOf` bridge. |
|
|
206
324
|
|
|
207
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()`.
|
|
208
326
|
|
|
209
|
-
At runtime the results are identical. At compile time, TypeScript picks whichever type system matches the exact identifier you used. If
|
|
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`.
|
|
210
328
|
|
|
211
329
|
---
|
|
212
330
|
|
|
@@ -236,7 +354,7 @@ declare module 'mnemonica' {
|
|
|
236
354
|
}
|
|
237
355
|
```
|
|
238
356
|
|
|
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.
|
|
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.
|
|
240
358
|
|
|
241
359
|
### What the builder mode can and cannot do
|
|
242
360
|
|
|
@@ -244,16 +362,14 @@ The builder mode solves the problem locally by carrying the registry in the retu
|
|
|
244
362
|
|
|
245
363
|
```typescript
|
|
246
364
|
const App = mnemonica.define('User', ...).define('Admin', ...);
|
|
247
|
-
// App's type is roughly { lookup: { User: ..., Admin: ... } }
|
|
365
|
+
// App's type is roughly { lookup: { User: ..., 'User.Admin': ... } }
|
|
248
366
|
```
|
|
249
367
|
|
|
250
|
-
This works because the function's return type can be computed from the generic parameters. The registry is local,
|
|
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).
|
|
251
369
|
|
|
252
|
-
|
|
370
|
+
What it still cannot handle:
|
|
253
371
|
|
|
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.
|
|
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.
|
|
257
373
|
|
|
258
374
|
### What the augmented mode can and cannot do
|
|
259
375
|
|
|
@@ -263,27 +379,27 @@ The augmented mode uses the global `TypeRegistry` interface. It can handle:
|
|
|
263
379
|
- `@decorate()` classes.
|
|
264
380
|
- Cross-file references because the interface is globally visible.
|
|
265
381
|
|
|
266
|
-
But it requires external tooling (Tactica) or hand-written
|
|
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`.
|
|
267
383
|
|
|
268
384
|
### Why not unify them?
|
|
269
385
|
|
|
270
|
-
The
|
|
386
|
+
The split is a consequence of TypeScript's design, not mnemonica's. You cannot have one mechanism that covers all three cases:
|
|
271
387
|
|
|
272
388
|
1. Chained builder calls.
|
|
273
389
|
2. Free function calls.
|
|
274
390
|
3. Decorated classes.
|
|
275
391
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
The runtime is the same. The difference is only in how TypeScript discovers the types.
|
|
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.
|
|
282
396
|
|
|
283
397
|
---
|
|
284
398
|
|
|
285
399
|
## Summary
|
|
286
400
|
|
|
287
|
-
- **
|
|
288
|
-
- **
|
|
289
|
-
- **
|
|
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
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(
|