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>() + }) + }) })