Generate Zod schema from CSV

Turn any CSV file into TypeScript interfaces or Zod schemas in one command — column headers become property names, types are inferred from the actual values.

Installation

npm install -D snaptype

Requires Node.js 20+. ESM only.

Basic usage

snaptype from-csv <file> [options]
# TypeScript interfaces (default)
snaptype from-csv data.csv -o src/types/row.ts

# Zod schema only
snaptype from-csv data.csv --zod -o src/schemas/row.ts

# Both TypeScript and Zod at once
snaptype from-csv data.csv --zod --ts -o src/types/row.ts

Example

Given a CSV file with this content:

Download users.csv
users.csv
id,email,role,score,createdAt
1,alice@example.com,admin,98.5,2024-01-15T10:30:00.000Z
2,bob@example.com,user,72.0,2024-03-22T08:00:00.000Z
3,carol@example.com,user,88.3,2024-06-10T14:00:00.000Z

snaptype generates:

src/types/row.ts
export interface Row {
  readonly id: number;
  email: string;       // email
  role: "admin" | "user";
  score: number;
  createdAt: string;   // ISO 8601
}

With --zod:

src/schemas/row.ts
import { z } from 'zod';

export const RowSchema = z.object({
  id: z.number(),
  email: z.email(),
  role: z.enum(["admin", "user"]),
  score: z.number(),
  createdAt: z.iso.datetime(),
}).readonly();

export type Row = z.infer<typeof RowSchema>;

Nullable fields

Empty cells are normalized to null and mark the field as nullable. A column where at least one row is empty becomes string | null (or the equivalent inferred type). This means the generated types accurately reflect real-world CSV exports that have missing values.

Files without a header row

If your CSV has no header row, provide column names manually with --columns.

snaptype from-csv data.csv --columns id,name,score,active -o src/types/row.ts

Options

FlagDescription
--columns <col1,col2,...>Column names when the file has no header row
--zodGenerate a Zod schema instead of (or alongside) TypeScript interfaces
--tsAlso emit a TypeScript file when --zod is set
-o, --output <path>Output file path (default: stdout)
-n, --naming <convention>Property naming: camel (default), pascal, snake
--readonlyAdd readonly to all properties
--emit <kind>Declaration style: interface (default) or type
-c, --config <path>Path to .snaptyperc config file

Watch mode Pro

Automatically regenerate types whenever the CSV file is saved. Useful during data exploration when your schema is still evolving.

snaptype from-csv data.csv --watch -o src/types/row.ts