Upgrade to Pro — share decks privately, control downloads, hide ads and more …

20250805 関数型DDD 15

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for saka saka
December 24, 2025
3

20250805 関数型DDD 15

Avatar for saka

saka

December 24, 2025

Transcript

  1. const assertNever = (_: never): never => { throw new

    Error(); }; type DogError = { kind: "DogError"; message: string; }; type CatError = { kind: "CatError"; message: string; }; type AnimalError = DogError | CatError;
  2. import { Result, ok, err } from "neverthrow"; const checkAnimal

    = (animal: string): Result<string, AnimalError> => { if (animal === "Dogn") { return err({ kind: "DogError", message: "Invalid animal" }); } if (animal === "Catn") { return err({ kind: "CatError", message: "Invalid animal" }); } return ok("Animal"); };
  3. const app = new Hono(); app.get("/", (c) => { return

    checkAnimal("Dog").match( (animal) => { return c.json({ message: animal }); }, (error) => { switch (error.kind) { case "DogError": return c.json({ error: `DogError: ${error.message}` }, 401); case "CatError": return c.json({ error: `CatError: ${error.message}` }, 400); default: return assertNever(error); } } ); });
  4. type InactiveUser = { // kind࢖ͬͨ΄͏͕͍͍ʁ id: UserId; email: Email;

    inactiveAt: InactiveAt; }; type DeletedUser = { // ຊ౰ʹ͜ͷܕ໊Ͱ͍͍ͷ͔ʁ id: UserId; email: Email; deletedAt: DeletedAt; };