15 — Data in Effect v4
Data.Class and Data.TaggedClass define immutable structural domain values; Data.TaggedError defines discriminated typed failures. V4 gives ordinary objects and tuples structural equality, so use normal literals for those shapes.
The exercises compare class instances with Equal.equals, compare plain values, and construct a tagged error. Choose a class when a named domain constructor adds meaning; keep incidental records as plain objects.
The full test suite
The index.test.ts for this lesson, verbatim from steps/15-data/.
import { describe, expect, it } from "vite-plus/test"import { Data, Equal } from "effect"
class User extends Data.Class<{ readonly id: string; readonly name: string }> {}class MissingUser extends Data.TaggedError("MissingUser")<{ readonly id: string }> {}
describe("Data", () => { it("gives classes structural equality", () => { expect(Equal.equals(new User({ id: "1", name: "Ada" }), new User({ id: "1", name: "Ada" }))).toBe(true) })
it("uses ordinary objects and tuples in v4", () => { expect(Equal.equals({ id: 1 }, { id: 1 })).toBe(true) expect(Equal.equals([1, 2], [1, 2])).toBe(true) })
it("creates discriminated domain errors", () => { expect(new MissingUser({ id: "9" })._tag).toBe("MissingUser") })})