From 04a3e070c19488992e6beb87e52f8ed66864bd3e Mon Sep 17 00:00:00 2001 From: Sebastian Lorenz Date: Mon, 5 Oct 2026 18:30:48 -0700 Subject: [PATCH] Type Atom.Serializable encode/decode as Schema.Json (#8763) Co-authored-by: Claude Opus 5.5 (1M context) Co-authored-by: Tim Smart --- .changeset/atom-serializable-json-types.md | 5 +++++ packages/effect/src/reactivity/Atom.ts | 3 ++- packages/effect/src/reactivity/AtomRegistry.ts | 2 +- packages/effect/typetest/reactivity/Atom.tst.ts | 10 ++++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 .changeset/atom-serializable-json-types.md diff --git a/.changeset/atom-serializable-json-types.md b/.changeset/atom-serializable-json-types.md new file mode 100644 index 0000000000..965bb9c480 --- /dev/null +++ b/.changeset/atom-serializable-json-types.md @@ -0,0 +1,5 @@ +--- +"effect": minor +--- + +Fix `Atom.Serializable` to encode to and decode from `Schema.Json`, matching the JSON codec used by `Atom.serializable`. Encoded results may now require narrowing; runtime behavior is unchanged. diff --git a/packages/effect/src/reactivity/Atom.ts b/packages/effect/src/reactivity/Atom.ts index ba695f774b..a6d262f3cf 100644 --- a/packages/effect/src/reactivity/Atom.ts +++ b/packages/effect/src/reactivity/Atom.ts @@ -2622,7 +2622,8 @@ export type SerializableTypeId = "~effect-atom/atom/Atom/Serializable" * **Details** * * The key identifies the atom in dehydrated state, and the encode/decode - * functions convert between the atom value and its JSON encoding. + * functions convert between the atom value and its JSON representation, as + * produced by the schema's JSON codec (`Schema.toCodecJson`). * * @stability unstable * @category models diff --git a/packages/effect/src/reactivity/AtomRegistry.ts b/packages/effect/src/reactivity/AtomRegistry.ts index ae5470276c..84cab6db73 100644 --- a/packages/effect/src/reactivity/AtomRegistry.ts +++ b/packages/effect/src/reactivity/AtomRegistry.ts @@ -474,7 +474,7 @@ class RegistryImpl implements AtomRegistry { this.removeNodeTimeout(node) } if (typeof key === "string" && this.preloadedSerializable.has(key)) { - const encoded = this.preloadedSerializable.get(key) + const encoded = this.preloadedSerializable.get(key) as Schema.Json this.preloadedSerializable.delete(key) const serializable = (atom as any as Atom.Serializable)[SerializableTypeId] let decoded: A diff --git a/packages/effect/typetest/reactivity/Atom.tst.ts b/packages/effect/typetest/reactivity/Atom.tst.ts index 648831a5eb..150fe0a105 100644 --- a/packages/effect/typetest/reactivity/Atom.tst.ts +++ b/packages/effect/typetest/reactivity/Atom.tst.ts @@ -67,4 +67,14 @@ describe("Atom", () => { expect(runtime.fn).type.toBeCallableWith(() => Effect.void, { reactivityKeys: keys }) }) }) + + describe("serializable", () => { + it("encodes to and decodes from the JSON representation", () => { + const atom = Atom.serializable(Atom.make(1n), { key: "bigint", schema: Schema.BigInt }) + const serializable = atom[Atom.SerializableTypeId] + + expect(serializable.encode).type.toBe<(value: bigint) => Schema.Json>() + expect(serializable.decode).type.toBe<(value: Schema.Json) => bigint>() + }) + }) })