|
| 1 | +# @tskm/example-advanced |
| 2 | + |
| 3 | +Three patterns past the [basic loop](../basic): **discriminated unions, recursive (cyclic) |
| 4 | +schemas, and the explicit `Infer` marker.** Each schema is materialized into a concrete |
| 5 | +`.gen.ts` type by `tskm gen`, so consuming it ([`src/main.ts`](src/main.ts)) costs the type |
| 6 | +system nothing. |
| 7 | + |
| 8 | +## 1. Discriminated union |
| 9 | + |
| 10 | +[`src/union.schema.ts`](src/union.schema.ts) — every member carries a `kind` literal: |
| 11 | + |
| 12 | +```ts |
| 13 | +export const shapeSchema = union([ |
| 14 | + object({ kind: literal("circle"), radius: number() }), |
| 15 | + object({ kind: literal("rectangle"), width: number(), height: number() }), |
| 16 | + object({ kind: literal("text"), content: string() }), |
| 17 | +]) |
| 18 | +``` |
| 19 | + |
| 20 | +generates the full union as a concrete type ([`union.schema.gen.ts`](src/union.schema.gen.ts)), |
| 21 | +which narrows on `kind` like any hand-written union: |
| 22 | + |
| 23 | +```ts |
| 24 | +export type Shape = { |
| 25 | + kind: "circle"; |
| 26 | + radius: number; |
| 27 | +} | { |
| 28 | + kind: "rectangle"; |
| 29 | + width: number; |
| 30 | + height: number; |
| 31 | +} | { |
| 32 | + kind: "text"; |
| 33 | + content: string; |
| 34 | +} |
| 35 | +``` |
| 36 | +
|
| 37 | +## 2. Recursive / cyclic schema — the special notation |
| 38 | +
|
| 39 | +[`src/json.schema.ts`](src/json.schema.ts) models a JSON value, which is both a **union** and |
| 40 | +**recursive** (a JSON value contains JSON values). TypeScript cannot *infer* a self-referential |
| 41 | +type, so a recursive schema needs three things that a normal schema does not: |
| 42 | +
|
| 43 | +1. **Hand-write the recursive type** — `Json` mentions `Json`. |
| 44 | +2. **Annotate the const** with `GenericSchema<Json>` — this breaks the inference cycle. |
| 45 | +3. **Wrap each self-reference in `lazy(() => …)`** — so the schema object can be built before |
| 46 | + it finishes referring to itself (the getter runs on first parse). |
| 47 | +
|
| 48 | +```ts |
| 49 | +export type Json = string | number | boolean | null | Json[] | { [key: string]: Json } |
| 50 | + |
| 51 | +export const jsonSchema: GenericSchema<Json> = union([ |
| 52 | + string(), |
| 53 | + number(), |
| 54 | + boolean(), |
| 55 | + null_(), |
| 56 | + array(lazy(() => jsonSchema)), |
| 57 | + record(lazy(() => jsonSchema)), |
| 58 | +]) |
| 59 | +``` |
| 60 | + |
| 61 | +`tskm gen` materializes a correct **self-referential** type ([`json.schema.gen.ts`](src/json.schema.gen.ts)): |
| 62 | + |
| 63 | +```ts |
| 64 | +export type Json = string | number | boolean | Json[] | { |
| 65 | + [x: string]: Json; |
| 66 | +} | null |
| 67 | +``` |
| 68 | +
|
| 69 | +> **The annotation is required, not optional.** Without `GenericSchema<Json>` the recursive |
| 70 | +> position silently degrades to `any` — tskm's fail-closed guard only inspects the top-level |
| 71 | +> type, not nested `any`, so codegen would emit a wrong type with no diagnostic. (At runtime |
| 72 | +> `lazy` follows the input's depth and is not cycle-guarded, so a pathologically deep value can |
| 73 | +> overflow the stack.) |
| 74 | +
|
| 75 | +## 3. The explicit `Infer` marker — opt-in discovery |
| 76 | +
|
| 77 | +tskm's auto-discovery is syntactic: it only finds a **direct** `export const x = object(…)` |
| 78 | +(or another tskm factory). A schema built by a **helper** is invisible to it. In |
| 79 | +[`src/book.schema.ts`](src/book.schema.ts) the const's initializer is a call to `makeEntity`, |
| 80 | +not a tskm factory: |
| 81 | +
|
| 82 | +```ts |
| 83 | +function makeEntity<E extends ObjectEntries>(entries: E) { |
| 84 | + return object({ id: string(), ...entries }) |
| 85 | +} |
| 86 | + |
| 87 | +export const bookSchema = makeEntity({ title: string(), pages: number() }) |
| 88 | + |
| 89 | +// Opt in explicitly — this marker is what `tskm gen` keys on. |
| 90 | +export type Book = Infer<typeof bookSchema> |
| 91 | +``` |
| 92 | +
|
| 93 | +`export type T = Infer<typeof schema>` (or `InferOutput<…>`) tells the compiler to materialize |
| 94 | +that schema anyway. It writes the concrete `Book` into [`book.schema.gen.ts`](src/book.schema.gen.ts); |
| 95 | +import `Book` from there. (`tskm gen --mode inplace` rewrites the marker *in place* instead of |
| 96 | +writing a sidecar — see the root README.) |
| 97 | +
|
| 98 | +## Generate |
| 99 | +
|
| 100 | +From the repository root (after `bun install` + `bun run build`): |
| 101 | +
|
| 102 | +```bash |
| 103 | +node packages/compiler/dist/cli.mjs gen --root examples/advanced |
| 104 | +# in a published project this is simply: npx tskm gen (or bunx tskm gen) |
| 105 | +``` |
| 106 | +
|
| 107 | +`tsconfig.json` maps `@tskm/core` to the workspace source via `paths` so the checker can resolve |
| 108 | +the inferred output type. In a real project `@tskm/core` is a normal dependency and no `paths` |
| 109 | +entry is needed. |
0 commit comments