mnemonica 1.2.1 → 1.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/FOR_HUMANS.md +186 -138
- package/README.md +70 -249
- package/build/api/errors/exceptionConstructor.js +20 -47
- package/build/api/errors/throwModificationError.js +26 -61
- package/build/api/types/InstanceCreator.js +5 -1
- package/build/api/types/Props.js +24 -10
- package/build/api/types/index.js +18 -12
- package/build/types/index.d.ts +9 -4
- package/build/types/index.js +1 -1
- package/docs/UTILS.md +97 -8
- package/docs/async-constructors.md +2 -0
- package/docs/decorate.md +1 -1
- package/docs/empathy-in-ai.md +20 -10
- package/docs/prototype-chain.md +8 -7
- package/docs/typed-lookup.md +195 -5
- package/docs/typeomatica.md +8 -345
- package/package.json +6 -10
- package/src/api/errors/exceptionConstructor.ts +40 -83
- package/src/api/errors/throwModificationError.ts +64 -104
- package/src/api/types/InstanceCreator.ts +14 -0
- package/src/api/types/Props.ts +53 -18
- package/src/api/types/index.ts +33 -20
- package/src/types/index.ts +15 -5
- package/.ai/AGENTS.md +0 -151
- package/.ai/ARCHITECT.md +0 -110
- package/.ai/DEBUG.md +0 -178
- package/.ai/ONBOARDING.md +0 -189
- package/.ai/PROTOTYPE-CHAIN.md +0 -153
- package/.ai/mode-ask.md +0 -121
- package/.ai/mode-orchestrator.md +0 -141
- package/.ai/performance-vs-security.md +0 -395
- package/.ai/rules-async-constructors.md +0 -352
- package/.ai/rules-code-style.md +0 -95
- package/.ai/rules-coding.md +0 -246
- package/.ai/rules-context-condensing.md +0 -50
- package/.ai/rules-contributing.md +0 -227
- package/.ai/rules-define-patterns.md +0 -159
- package/.ai/rules-ecosystem.md +0 -140
- package/.ai/rules-error-system.md +0 -56
- package/.ai/rules-hooks.md +0 -46
- package/.ai/rules-instance-methods.md +0 -93
- package/.ai/rules-philosophy.md +0 -231
- package/.ai/rules-reminders.md +0 -19
- package/.ai/rules-testing.md +0 -66
- package/.ai/rules-type-system.md +0 -124
- package/.ai/theory-of-operations.md +0 -224
- package/AGENTS.md +0 -345
- package/CONTRIBUTING.md +0 -150
- package/SKILL.md +0 -114
- package/docs/ai-learning-trajectory.md +0 -141
- package/docs/purpose.md +0 -707
- package/docs/tactica-deep-dive.md +0 -629
- package/docs/tactica-pattern.md +0 -146
|
@@ -1,629 +0,0 @@
|
|
|
1
|
-
# The mnemonica + tactica Type-Safe Pattern
|
|
2
|
-
|
|
3
|
-
> **Project-agnostic reference.** This document applies to every project using mnemonica with tactica.
|
|
4
|
-
> Read it when you are tempted to write `as unknown as` with mnemonica types.
|
|
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
|
-
|
|
13
|
-
---
|
|
14
|
-
|
|
15
|
-
## Table of Contents
|
|
16
|
-
|
|
17
|
-
1. [The Runtime Reality: mnemonica Is a Trie](#1-the-runtime-reality-mnemonica-is-a-trie)
|
|
18
|
-
2. [The TypeScript Gap](#2-the-typescript-gap)
|
|
19
|
-
3. [How tactica Bridges the Gap](#3-how-tactica-bridges-the-gap)
|
|
20
|
-
4. [Declaration Merging: The TypeScript Mechanism](#4-declaration-merging-the-typescript-mechanism)
|
|
21
|
-
5. [tsconfig.json Setup](#5-tsconfigjson-setup)
|
|
22
|
-
6. [How lookup Works](#6-how-lookup-works)
|
|
23
|
-
7. [Why Direct Import Fails](#7-why-direct-import-fails)
|
|
24
|
-
8. [The Epiphany: Zero-Cast Chaining](#8-the-epiphany-zero-cast-chaining)
|
|
25
|
-
9. [Common Mistakes](#9-common-mistakes)
|
|
26
|
-
10. [Cheat Sheet](#10-cheat-sheet)
|
|
27
|
-
|
|
28
|
-
---
|
|
29
|
-
|
|
30
|
-
## 1. The Runtime Reality: mnemonica Is a Trie
|
|
31
|
-
|
|
32
|
-
At runtime, mnemonica creates a **Trie data structure** of constructors.
|
|
33
|
-
|
|
34
|
-
```typescript
|
|
35
|
-
// You define a type
|
|
36
|
-
const RequestData = define('RequestData', function (this: {...}, data) {
|
|
37
|
-
Object.assign(this, data);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
// You define a child type
|
|
41
|
-
const RouteData = RequestData.define('RouteData', function (this: {...}, data) {
|
|
42
|
-
Object.assign(this, data);
|
|
43
|
-
});
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
What exists at runtime:
|
|
47
|
-
|
|
48
|
-
```
|
|
49
|
-
RequestData (constructor)
|
|
50
|
-
├── RouteData (sub-constructor, accessible as RequestData.RouteData)
|
|
51
|
-
│ ├── PageData (sub-constructor)
|
|
52
|
-
│ │ ├── RenderData (sub-constructor)
|
|
53
|
-
│ │ │ └── ResponseData (sub-constructor)
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
When you call `new RequestData(data)`, mnemonica:
|
|
57
|
-
1. Creates an instance with the prototype chain set
|
|
58
|
-
2. Attaches every sub-constructor as a property on the instance
|
|
59
|
-
3. The instance itself can spawn children: `new requestData.RouteData(data)`
|
|
60
|
-
|
|
61
|
-
**This is runtime behavior.** It works in JavaScript without any TypeScript involvement.
|
|
62
|
-
|
|
63
|
-
---
|
|
64
|
-
|
|
65
|
-
## 2. The TypeScript Gap
|
|
66
|
-
|
|
67
|
-
TypeScript does **not** know about the Trie structure by default.
|
|
68
|
-
|
|
69
|
-
When you write:
|
|
70
|
-
|
|
71
|
-
```typescript
|
|
72
|
-
import { RequestData } from './collections/requestTypes.js';
|
|
73
|
-
const requestData = new RequestData({ method: 'GET', url: '/' });
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
TypeScript sees `RequestData` as a constructor function returned by `define()`. It knows the return type is `RequestData` (the instance type), but it does **not** know that:
|
|
77
|
-
- `RequestData` has a `.RouteData` property
|
|
78
|
-
- The instance `requestData` has a `.RouteData` sub-constructor
|
|
79
|
-
- `.RouteData` returns an instance with `.PageData`
|
|
80
|
-
- And so on through the chain
|
|
81
|
-
|
|
82
|
-
So when you write:
|
|
83
|
-
|
|
84
|
-
```typescript
|
|
85
|
-
const routeData = new requestData.RouteData({ pagePath: '/' });
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
TypeScript complains: `Property 'RouteData' does not exist on type 'RequestData'`.
|
|
89
|
-
|
|
90
|
-
This is the gap: **mnemonica knows about the Trie, but TypeScript doesn't.**
|
|
91
|
-
|
|
92
|
-
---
|
|
93
|
-
|
|
94
|
-
## 3. How tactica Bridges the Gap
|
|
95
|
-
|
|
96
|
-
tactica is an AST analyzer. It scans your codebase for `define()` calls and understands:
|
|
97
|
-
- The type name (`'RequestData'`)
|
|
98
|
-
- The parent type (if any)
|
|
99
|
-
- The `this` properties
|
|
100
|
-
- The constructor parameters
|
|
101
|
-
- The full inheritance chain
|
|
102
|
-
|
|
103
|
-
It generates two files in `.tactica/`:
|
|
104
|
-
|
|
105
|
-
### 3.1 `.tactica/types.ts`
|
|
106
|
-
|
|
107
|
-
Contains the instance types:
|
|
108
|
-
|
|
109
|
-
```typescript
|
|
110
|
-
export type RequestData = {
|
|
111
|
-
method: string;
|
|
112
|
-
url: string;
|
|
113
|
-
query: Record<string, unknown>;
|
|
114
|
-
// ...
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
export type RequestData_RouteData = ProtoFlat<RequestData, {
|
|
118
|
-
pagePath: string;
|
|
119
|
-
isMain: boolean;
|
|
120
|
-
deep: string;
|
|
121
|
-
}>;
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
`ProtoFlat<A, B>` merges the properties of parent `A` and child `B`.
|
|
125
|
-
|
|
126
|
-
### 3.2 `.tactica/registry.ts`
|
|
127
|
-
|
|
128
|
-
Contains the **TypeRegistry augmentation**:
|
|
129
|
-
|
|
130
|
-
```typescript
|
|
131
|
-
declare module 'mnemonica' {
|
|
132
|
-
interface TypeRegistry {
|
|
133
|
-
'RequestData': new (...args: unknown[]) => RequestData;
|
|
134
|
-
'RequestData_RouteData': new (...args: unknown[]) => RequestData_RouteData;
|
|
135
|
-
'RequestData_RouteData_PageData': new (...args: unknown[]) => RequestData_RouteData_PageData;
|
|
136
|
-
// ... every type in every chain
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
**This is the critical file.** It teaches TypeScript about the constructors.
|
|
142
|
-
|
|
143
|
-
---
|
|
144
|
-
|
|
145
|
-
## 4. Declaration Merging: The TypeScript Mechanism
|
|
146
|
-
|
|
147
|
-
To understand why `.tactica/registry.ts` works, you need to understand **TypeScript declaration merging**.
|
|
148
|
-
|
|
149
|
-
### 4.1 What Is Declaration Merging?
|
|
150
|
-
|
|
151
|
-
TypeScript allows multiple declarations with the same name to be **merged** into a single definition:
|
|
152
|
-
|
|
153
|
-
```typescript
|
|
154
|
-
// File A.ts
|
|
155
|
-
interface Person {
|
|
156
|
-
name: string;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// File B.ts
|
|
160
|
-
interface Person {
|
|
161
|
-
age: number;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// Result: Person has both name and age
|
|
165
|
-
const p: Person = { name: 'Alice', age: 30 }; // OK
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
This works for:
|
|
169
|
-
- Interfaces (merged member-wise)
|
|
170
|
-
- Namespaces (merged member-wise)
|
|
171
|
-
- Classes with namespaces
|
|
172
|
-
- **Module augmentations** ← this is what tactica uses
|
|
173
|
-
|
|
174
|
-
### 4.2 Module Augmentation
|
|
175
|
-
|
|
176
|
-
You can augment an existing module's exports using `declare module`:
|
|
177
|
-
|
|
178
|
-
```typescript
|
|
179
|
-
// Original module
|
|
180
|
-
// node_modules/some-lib/index.d.ts
|
|
181
|
-
export interface Config {
|
|
182
|
-
timeout: number;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// Your augmentation
|
|
186
|
-
// src/augmentation.ts
|
|
187
|
-
declare module 'some-lib' {
|
|
188
|
-
interface Config {
|
|
189
|
-
retries: number;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// Now Config has both timeout and retries
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
### 4.3 How tactica Uses It
|
|
197
|
-
|
|
198
|
-
mnemonica core defines an empty `TypeRegistry`:
|
|
199
|
-
|
|
200
|
-
```typescript
|
|
201
|
-
// In mnemonica core (src/index.ts)
|
|
202
|
-
export interface TypeRegistry {
|
|
203
|
-
// Intentionally empty. Augment via declaration merging.
|
|
204
|
-
}
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
tactica generates:
|
|
208
|
-
|
|
209
|
-
```typescript
|
|
210
|
-
// In .tactica/registry.ts
|
|
211
|
-
declare module 'mnemonica' {
|
|
212
|
-
interface TypeRegistry {
|
|
213
|
-
'RequestData': TypeConstructor<RequestData>;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
After this augmentation, `TypeRegistry` contains the `'RequestData'` key. Any code that imports from `'mnemonica'` sees the augmented `TypeRegistry`.
|
|
219
|
-
|
|
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`?
|
|
232
|
-
|
|
233
|
-
The base `TypeRegistry` is intentionally empty:
|
|
234
|
-
|
|
235
|
-
```typescript
|
|
236
|
-
export interface TypeRegistry {}
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
Without augmentation, `lookup('Something')` falls back to the broad `TypeClass | undefined` return type. This keeps the library usable without a registry while still allowing full type inference once you augment it.
|
|
240
|
-
|
|
241
|
-
Once augmented:
|
|
242
|
-
|
|
243
|
-
```typescript
|
|
244
|
-
declare module 'mnemonica' {
|
|
245
|
-
interface TypeRegistry {
|
|
246
|
-
'RequestData': TypeConstructor<RequestData>;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
const RequestData = lookup('RequestData'); // typed constructor
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
---
|
|
254
|
-
|
|
255
|
-
## 5. tsconfig.json Setup
|
|
256
|
-
|
|
257
|
-
For declaration merging to work, the `.tactica/registry.ts` file must be included in your TypeScript compilation.
|
|
258
|
-
|
|
259
|
-
### 5.1 Required Configuration
|
|
260
|
-
|
|
261
|
-
```json
|
|
262
|
-
{
|
|
263
|
-
"compilerOptions": {
|
|
264
|
-
"module": "NodeNext",
|
|
265
|
-
"moduleResolution": "NodeNext",
|
|
266
|
-
"strict": true,
|
|
267
|
-
"esModuleInterop": true
|
|
268
|
-
},
|
|
269
|
-
"include": [
|
|
270
|
-
"src/**/*",
|
|
271
|
-
".tactica/**/*"
|
|
272
|
-
]
|
|
273
|
-
}
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
**Critical points:**
|
|
277
|
-
|
|
278
|
-
1. **`.tactica` must be in `include`** — Otherwise TypeScript never sees the augmentation.
|
|
279
|
-
|
|
280
|
-
2. **`strict: true`** — Ensures the type system is fully active.
|
|
281
|
-
|
|
282
|
-
3. **`moduleResolution: "NodeNext"`** — Required for modern ESM resolution with `.js` extensions.
|
|
283
|
-
|
|
284
|
-
### 5.2 Why `include` Matters
|
|
285
|
-
|
|
286
|
-
TypeScript only processes files in the `include` array. If `.tactica/` is not included:
|
|
287
|
-
|
|
288
|
-
```typescript
|
|
289
|
-
import { lookup } from 'mnemonica';
|
|
290
|
-
const RequestData = lookup('RequestData');
|
|
291
|
-
// RequestData is typed as TypeClass | undefined instead of the concrete constructor
|
|
292
|
-
```
|
|
293
|
-
|
|
294
|
-
Because without the augmentation, `TypeRegistry` is empty and `lookup()` falls back to its unaugmented overload.
|
|
295
|
-
|
|
296
|
-
---
|
|
297
|
-
|
|
298
|
-
## 6. How lookup Works
|
|
299
|
-
|
|
300
|
-
### 6.1 Runtime Behavior
|
|
301
|
-
|
|
302
|
-
```typescript
|
|
303
|
-
// In mnemonica core (src/index.ts)
|
|
304
|
-
export function lookup<const K extends keyof TypeRegistry>(
|
|
305
|
-
this: unknown,
|
|
306
|
-
TypeNestedPath: K
|
|
307
|
-
): TypeRegistry[K];
|
|
308
|
-
export function lookup(
|
|
309
|
-
this: unknown,
|
|
310
|
-
TypeNestedPath: string
|
|
311
|
-
): TypeClass | undefined {
|
|
312
|
-
// Runtime delegates to types.lookup()
|
|
313
|
-
const types = checkThis(this) ? defaultTypes : this || defaultTypes;
|
|
314
|
-
return types.lookup(TypeNestedPath);
|
|
315
|
-
}
|
|
316
|
-
};
|
|
317
|
-
```
|
|
318
|
-
|
|
319
|
-
At runtime, `lookup('RequestData')`:
|
|
320
|
-
1. Calls `types.lookup('RequestData')`
|
|
321
|
-
2. Searches the default types collection
|
|
322
|
-
3. Returns the constructor function
|
|
323
|
-
|
|
324
|
-
**This is the same constructor as the direct import.** No difference at runtime.
|
|
325
|
-
|
|
326
|
-
### 6.2 Compile-Time Behavior
|
|
327
|
-
|
|
328
|
-
At compile time, TypeScript sees:
|
|
329
|
-
|
|
330
|
-
```typescript
|
|
331
|
-
const RequestData = lookup('RequestData');
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
TypeScript infers:
|
|
335
|
-
- `K` = `'RequestData'` (literal type, thanks to `const`)
|
|
336
|
-
- `keyof TypeRegistry` = all registered type names
|
|
337
|
-
- `TypeRegistry['RequestData']` = `new (...args: unknown[]) => RequestData`
|
|
338
|
-
|
|
339
|
-
So `RequestData` has type `new (...args: unknown[]) => RequestData`.
|
|
340
|
-
|
|
341
|
-
When you then write:
|
|
342
|
-
|
|
343
|
-
```typescript
|
|
344
|
-
const requestData = new RequestData({ method: 'GET', url: '/' });
|
|
345
|
-
```
|
|
346
|
-
|
|
347
|
-
TypeScript knows `requestData` is of type `RequestData` (the instance type).
|
|
348
|
-
|
|
349
|
-
### 6.3 The Constructor Has Sub-Constructors
|
|
350
|
-
|
|
351
|
-
But here's the key: the `.tactica/types.ts` file also defines the instance type to have sub-constructors:
|
|
352
|
-
|
|
353
|
-
```typescript
|
|
354
|
-
// This is part of how ProtoFlat and the generated types work
|
|
355
|
-
// The RequestData instance type KNOWS it has .RouteData
|
|
356
|
-
```
|
|
357
|
-
|
|
358
|
-
When TypeScript sees:
|
|
359
|
-
|
|
360
|
-
```typescript
|
|
361
|
-
const routeData = new requestData.RouteData({ pagePath: '/' });
|
|
362
|
-
```
|
|
363
|
-
|
|
364
|
-
It checks the type of `requestData` → `RequestData`.
|
|
365
|
-
It checks if `RequestData` has `.RouteData` → Yes, in the generated type.
|
|
366
|
-
It checks the constructor signature → `new (...args: unknown[]) => RequestData_RouteData`.
|
|
367
|
-
|
|
368
|
-
**No cast needed.** TypeScript believes you because the type system was told the truth.
|
|
369
|
-
|
|
370
|
-
---
|
|
371
|
-
|
|
372
|
-
## 7. Why Direct Import Fails
|
|
373
|
-
|
|
374
|
-
Let's trace what happens with direct import vs `lookup`.
|
|
375
|
-
|
|
376
|
-
### 7.1 Direct Import
|
|
377
|
-
|
|
378
|
-
```typescript
|
|
379
|
-
import { RequestData } from './collections/requestTypes.js';
|
|
380
|
-
// ↑
|
|
381
|
-
// TypeScript sees: typeof RequestData (the constructor)
|
|
382
|
-
// It knows: new (...args: unknown[]) => RequestData
|
|
383
|
-
// It does NOT know: RequestData.RouteData exists
|
|
384
|
-
|
|
385
|
-
const requestData = new RequestData({ ... });
|
|
386
|
-
// TypeScript: requestData is RequestData
|
|
387
|
-
|
|
388
|
-
const routeData = new requestData.RouteData({ ... });
|
|
389
|
-
// TypeScript ERROR: Property 'RouteData' does not exist on type 'RequestData'
|
|
390
|
-
```
|
|
391
|
-
|
|
392
|
-
The direct import returns the constructor, but TypeScript's type for that constructor doesn't include `.RouteData`. The `define()` function's return type is not augmented with sub-constructor information.
|
|
393
|
-
|
|
394
|
-
### 7.2 lookup
|
|
395
|
-
|
|
396
|
-
```typescript
|
|
397
|
-
import { lookup } from 'mnemonica';
|
|
398
|
-
|
|
399
|
-
const RequestData = lookup('RequestData');
|
|
400
|
-
// ↑
|
|
401
|
-
// TypeScript sees: TypeRegistry['RequestData']
|
|
402
|
-
// Which is: new (...args: unknown[]) => RequestData
|
|
403
|
-
// PLUS the instance type RequestData knows about .RouteData
|
|
404
|
-
|
|
405
|
-
const requestData = new RequestData({ ... });
|
|
406
|
-
// TypeScript: requestData is RequestData
|
|
407
|
-
|
|
408
|
-
const routeData = new requestData.RouteData({ ... });
|
|
409
|
-
// TypeScript: OK! RouteData is a known property of RequestData instance
|
|
410
|
-
```
|
|
411
|
-
|
|
412
|
-
The difference is not in the runtime object. The difference is in **TypeScript's compile-time knowledge**.
|
|
413
|
-
|
|
414
|
-
---
|
|
415
|
-
|
|
416
|
-
## 8. The Epiphany: Zero-Cast Chaining
|
|
417
|
-
|
|
418
|
-
The complete pattern, end to end:
|
|
419
|
-
|
|
420
|
-
### Step 1: Define
|
|
421
|
-
|
|
422
|
-
```typescript
|
|
423
|
-
// src/collections/requestTypes.ts
|
|
424
|
-
import { define } from 'mnemonica';
|
|
425
|
-
|
|
426
|
-
export const RequestData = define('RequestData', function (
|
|
427
|
-
this: { method: string; url: string },
|
|
428
|
-
data: { method: string; url: string }
|
|
429
|
-
) {
|
|
430
|
-
Object.assign(this, data);
|
|
431
|
-
});
|
|
432
|
-
|
|
433
|
-
export const RouteData = RequestData.define('RouteData', function (
|
|
434
|
-
this: { pagePath: string },
|
|
435
|
-
data: { pagePath: string }
|
|
436
|
-
) {
|
|
437
|
-
Object.assign(this, data);
|
|
438
|
-
});
|
|
439
|
-
```
|
|
440
|
-
|
|
441
|
-
### Step 2: Generate
|
|
442
|
-
|
|
443
|
-
```bash
|
|
444
|
-
npx tactica --esm --verbose
|
|
445
|
-
```
|
|
446
|
-
|
|
447
|
-
Creates:
|
|
448
|
-
- `.tactica/types.ts` — instance types
|
|
449
|
-
- `.tactica/registry.ts` — TypeRegistry augmentation
|
|
450
|
-
|
|
451
|
-
### Step 3: Lookup
|
|
452
|
-
|
|
453
|
-
```typescript
|
|
454
|
-
// src/server.ts or any file
|
|
455
|
-
import { lookup } from 'mnemonica';
|
|
456
|
-
|
|
457
|
-
const RequestData = lookup('RequestData');
|
|
458
|
-
```
|
|
459
|
-
|
|
460
|
-
### Step 4: Chain (Zero Casts)
|
|
461
|
-
|
|
462
|
-
```typescript
|
|
463
|
-
const requestData = new RequestData({ method: 'GET', url: '/' });
|
|
464
|
-
const routeData = new requestData.RouteData({ pagePath: '/' });
|
|
465
|
-
const pageData = new routeData.PageData({ header: {...}, content: '' });
|
|
466
|
-
// ... and so on
|
|
467
|
-
```
|
|
468
|
-
|
|
469
|
-
**Every `.SubType` access is fully typed.** TypeScript knows:
|
|
470
|
-
- The constructor signature
|
|
471
|
-
- The instance properties
|
|
472
|
-
- The next sub-constructor in the chain
|
|
473
|
-
|
|
474
|
-
---
|
|
475
|
-
|
|
476
|
-
## 9. Common Mistakes
|
|
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
|
-
|
|
503
|
-
### 9.1 "I'll just cast it"
|
|
504
|
-
|
|
505
|
-
```typescript
|
|
506
|
-
// ❌ WRONG
|
|
507
|
-
const requestData = new RequestData({ ... }) as unknown as RequestDataT;
|
|
508
|
-
```
|
|
509
|
-
|
|
510
|
-
**Why wrong:** You are fighting the type system instead of using it. Every cast is a bug waiting to happen. If the type changes, the cast still compiles but may break at runtime.
|
|
511
|
-
|
|
512
|
-
**Fix:** Use `lookup`.
|
|
513
|
-
|
|
514
|
-
### 9.2 "I'll import the generated types too"
|
|
515
|
-
|
|
516
|
-
```typescript
|
|
517
|
-
// ❌ WRONG
|
|
518
|
-
import { RequestData } from './collections/requestTypes.js';
|
|
519
|
-
import type { RequestData as RequestDataT } from '../../.tactica/types.js';
|
|
520
|
-
const requestData = new RequestData({ ... }) as unknown as RequestDataT;
|
|
521
|
-
```
|
|
522
|
-
|
|
523
|
-
**Why wrong:** You are importing both the runtime constructor AND the generated type, then bridging them with a cast. This is twice the work and still unsafe.
|
|
524
|
-
|
|
525
|
-
**Fix:** Use `lookup` — it gives you both the runtime constructor AND the type in one call.
|
|
526
|
-
|
|
527
|
-
### 9.3 "lookup only works in route handlers"
|
|
528
|
-
|
|
529
|
-
```typescript
|
|
530
|
-
// ❌ WRONG (unnecessary)
|
|
531
|
-
app.get('/test', async () => {
|
|
532
|
-
const RequestData = lookup('RequestData');
|
|
533
|
-
const requestData = new RequestData({ ... });
|
|
534
|
-
});
|
|
535
|
-
```
|
|
536
|
-
|
|
537
|
-
**Why wrong:** `lookup` is a runtime lookup, but it's deterministic and cached. Calling it at module level is perfectly fine and more efficient:
|
|
538
|
-
|
|
539
|
-
```typescript
|
|
540
|
-
// ✅ CORRECT
|
|
541
|
-
const RequestData = lookup('RequestData');
|
|
542
|
-
|
|
543
|
-
app.get('/test', async () => {
|
|
544
|
-
const requestData = new RequestData({ ... });
|
|
545
|
-
});
|
|
546
|
-
```
|
|
547
|
-
|
|
548
|
-
### 9.4 "I need to import the constructor for decoration"
|
|
549
|
-
|
|
550
|
-
```typescript
|
|
551
|
-
// ❌ WRONG (mixing patterns)
|
|
552
|
-
import { RequestData } from './collections/requestTypes.js';
|
|
553
|
-
const TypedRequestData = lookup('RequestData');
|
|
554
|
-
|
|
555
|
-
app.decorate('RequestData', RequestData); // direct import
|
|
556
|
-
const requestData = new TypedRequestData({ ... }); // lookup
|
|
557
|
-
```
|
|
558
|
-
|
|
559
|
-
**Why wrong:** You're maintaining two references to the same object.
|
|
560
|
-
|
|
561
|
-
**Fix:** Use `lookup` for everything. The returned constructor is the same object:
|
|
562
|
-
|
|
563
|
-
```typescript
|
|
564
|
-
// ✅ CORRECT
|
|
565
|
-
const RequestData = lookup('RequestData');
|
|
566
|
-
|
|
567
|
-
app.decorate('RequestData', RequestData);
|
|
568
|
-
const requestData = new RequestData({ ... });
|
|
569
|
-
```
|
|
570
|
-
|
|
571
|
-
### 9.5 Forgetting to regenerate
|
|
572
|
-
|
|
573
|
-
```typescript
|
|
574
|
-
// You add a new property to the define() call
|
|
575
|
-
// But forget to run tactica
|
|
576
|
-
const RequestData = lookup('RequestData');
|
|
577
|
-
const requestData = new RequestData({ newField: 'value' });
|
|
578
|
-
// Error: Object literal may only specify known properties
|
|
579
|
-
```
|
|
580
|
-
|
|
581
|
-
**Fix:** Always regenerate after modifying `define()` calls:
|
|
582
|
-
|
|
583
|
-
```bash
|
|
584
|
-
npm run tactica
|
|
585
|
-
```
|
|
586
|
-
|
|
587
|
-
---
|
|
588
|
-
|
|
589
|
-
## 10. Cheat Sheet
|
|
590
|
-
|
|
591
|
-
| I want to... | Do this | Don't do this |
|
|
592
|
-
|---|---|---|
|
|
593
|
-
| Get a typed constructor | `const T = lookup('T')` | `import { T } from './collections/T.js'` |
|
|
594
|
-
| Create an instance | `new T({ ... })` | `new T({ ... }) as unknown as TT` |
|
|
595
|
-
| Chain to a child type | `new instance.Child({ ... })` | `new (instance as any).Child({ ... })` |
|
|
596
|
-
| Decorate Fastify | `app.decorate('T', T)` with `lookup` | Direct import + separate lookup |
|
|
597
|
-
| Add a new property | Modify `define()` → run `tactica` | Modify `define()` + manual cast |
|
|
598
|
-
| Fix "Property does not exist" | Run `tactica` to regenerate | Add `as any` or `as unknown` |
|
|
599
|
-
|
|
600
|
-
### Build Commands
|
|
601
|
-
|
|
602
|
-
```bash
|
|
603
|
-
# Regenerate types after modifying define() calls
|
|
604
|
-
npm run tactica
|
|
605
|
-
|
|
606
|
-
# Verify everything compiles
|
|
607
|
-
npx tsc --noEmit
|
|
608
|
-
|
|
609
|
-
# Verify linting
|
|
610
|
-
npx eslint src/
|
|
611
|
-
|
|
612
|
-
# Run tests
|
|
613
|
-
npx vitest run
|
|
614
|
-
```
|
|
615
|
-
|
|
616
|
-
---
|
|
617
|
-
|
|
618
|
-
## Trust
|
|
619
|
-
|
|
620
|
-
This pattern works because:
|
|
621
|
-
|
|
622
|
-
1. **mnemonica creates a Trie at runtime** — sub-constructors exist, instances can spawn children
|
|
623
|
-
2. **tactica teaches TypeScript about the Trie** — via declaration merging of `TypeRegistry`
|
|
624
|
-
3. **lookup retrieves the typed constructor** — compile-time type safety + runtime correctness
|
|
625
|
-
4. **The same object is returned either way** — `import { T }` and `lookup('T')` are identical at runtime
|
|
626
|
-
|
|
627
|
-
**The only difference is TypeScript's knowledge.** Use `lookup` and let TypeScript help you.
|
|
628
|
-
|
|
629
|
-
If you find yourself writing `as unknown as` with mnemonica types, **you have taken a wrong turn.** Stop. Use `lookup`. Trust the registry.
|