From f43a0d26f9e32cdef8fbd0c38aaa8c505e414073 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 11 Oct 2026 03:51:11 +0200 Subject: [PATCH] cultcache-ts codec: unit arm is a frozen object, recursive test codec annotated Co-Authored-By: Claude Opus 5.5 --- packages/cultcache-ts/src/codec.ts | 13 ++++++++----- packages/cultcache-ts/test/union-parity.test.ts | 3 ++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/cultcache-ts/src/codec.ts b/packages/cultcache-ts/src/codec.ts index 9e3a1552..44138bc9 100644 --- a/packages/cultcache-ts/src/codec.ts +++ b/packages/cultcache-ts/src/codec.ts @@ -158,9 +158,12 @@ export function slots(spec: SlotsSpec): SlotsCode } // An arm with no slots. It is written [key, []], never [key, nil]. -export const unit = Symbol("cultcache.unit-arm"); +export interface UnitArm { + readonly unit: true; +} +export const unit: UnitArm = Object.freeze({ unit: true }); -type ArmSpec = readonly [armName: TKind, body: SlotsCodec | typeof unit]; +type ArmSpec = readonly [armName: TKind, body: SlotsCodec | UnitArm]; // A tagged union written [armKey, [armSlots...]]. The reader refuses an undeclared key, naming the union, the key and // the declared keys; there is no fallback arm. @@ -172,14 +175,14 @@ export function union( if (keys.length === 0) { throw new Error(`Union ${name} declares no arms.`); } - const byKey = new Map | typeof unit }>(); + const byKey = new Map | UnitArm }>(); const byName = new Map(); for (const key of keys) { const [armName, body] = arms[key]; if (byName.has(armName)) { throw new Error(`Union ${name} declares the arm "${armName}" twice.`); } - if (body !== unit && body.names.includes("kind")) { + if (body !== unit && (body as SlotsCodec).names.includes("kind")) { throw new Error(`Union ${name} arm "${armName}" declares a slot named kind, which is the union's own tag.`); } byKey.set(key, { armName, body }); @@ -197,7 +200,7 @@ export function union( throw new Error(`Union ${name}: the value's kind is not one of its arms (${[...byName.keys()].join(", ")}).`); } const { armName, body } = byKey.get(key)!; - return [key, body === unit ? [] : at(`arm ${armName}`, () => body.encode(value))]; + return [key, body === unit ? [] : at(`arm ${armName}`, () => (body as SlotsCodec).encode(value))]; }, decode(raw) { if (raw === null) { diff --git a/packages/cultcache-ts/test/union-parity.test.ts b/packages/cultcache-ts/test/union-parity.test.ts index 801cff93..560b7273 100644 --- a/packages/cultcache-ts/test/union-parity.test.ts +++ b/packages/cultcache-ts/test/union-parity.test.ts @@ -8,6 +8,7 @@ import { decode, encode } from "@msgpack/msgpack"; import { z } from "zod"; import { codecs } from "../src"; +import type { Codec } from "../src/codec"; import { CultCache } from "../src/cult-cache"; import { defineDocumentType } from "../src/document"; import { SingleFileMessagePackBackingStore } from "../src/single-file-messagepack-backing-store"; @@ -43,7 +44,7 @@ interface Surface { } const composition = slots({ 0: ["name", string], 1: ["body", lazy(() => node)] }); -const node = union("Node", { +const node: Codec = union("Node", { 0: ["Box", slots<{ id: string; children: (Node | null)[] }>({ 0: ["id", string], 1: ["children", list(lazy(() => node))] })], 1: ["Text", slots<{ id: string; content: string }>({ 0: ["id", string], 1: ["content", string] })], 2: ["Use", slots<{ id: string; composition: Composition; slots: (Node | null)[] }>({