[[Zod]]でよく使う書き方をまとめる。 ## 基本 ```ts import { z } from "zod"; // 定義 const ZMember = z.object({ name: z.string(), mailAddress: z.string(), }); // 型定義は同名なものをexport export type Member = z.infer<typeof ZMember>; // .parseでJson Objectをパースして変換 // 定義に従っていないとエラーになる const response = ZMember.parse(jsonObj) ``` ## enum https://github.com/colinhacks/zod#zod-enums ```ts const Color = z.enum(["red", "green", "blue"]); export type Color = z.infer<typeof Color>; ``` ## Array https://github.com/colinhacks/zod#arrays ```ts const Item = z.object({}); export type Item = z.infer<typeof Item>; const Response = z.object({ items: Item.array(), strings: z.string().array(), }) ```