Skip to content

prefer-zod-parse-output-type

Derive a function's return contract from the local Zod schema whose parsed output it returns.

Why

A hand-written contract can drift from the runtime-validated value even while each declaration remains locally valid.

Fix

Export or colocate the schema and derive the contract with z.output<typeof Schema>.

Examples

Before — flagged Do not hand-write the parsed return shape
src/contracts.ts
export interface ParsedRow {
id: string;
}
src/row.ts
import { z } from "zod";
import type { ParsedRow } from "./contracts.js";
const RowSchema = z.object({ id: z.string() });
function load(): ParsedRow {
return RowSchema.parse({});
}
After — preferred Derive the validated return contract
src/row.ts
import { z } from "zod";
const RowSchema = z.object({ id: z.string() });
type Row = z.output<typeof RowSchema>;
function load(): Row {
return RowSchema.parse({});
}