Generate TypeScript types from JSON
Turn any JSON file into TypeScript interfaces or Zod schemas in one command — with automatic semantic inference and enum detection.
Installation
npm install -D snaptypeRequires Node.js 20+. ESM only.
Basic usage
Pass any JSON file to from-json and snaptype infers the TypeScript type from the actual values.
snaptype from-json <file> [options]# TypeScript interfaces (default)
snaptype from-json users.json -o src/types/user.ts
# Zod schema only
snaptype from-json users.json --zod -o src/schemas/user.ts
# Both TypeScript and Zod at once
snaptype from-json users.json --zod --ts -o src/types/user.tsExample
Given a JSON file with this structure:
Download users.json[
{
"id": 1,
"email": "alice@example.com",
"role": "admin",
"createdAt": "2024-01-15T10:30:00.000Z"
},
{
"id": 2,
"email": "bob@example.com",
"role": "user",
"createdAt": "2024-03-22T08:00:00.000Z"
}
]snaptype generates:
export interface User {
readonly id: number;
email: string; // email
role: "admin" | "user";
createdAt: string; // ISO 8601
}With --zod:
import { z } from 'zod';
export const UserSchema = z.object({
id: z.number(),
email: z.email(),
role: z.enum(["admin", "user"]),
createdAt: z.iso.datetime(),
}).readonly();
export type User = z.infer<typeof UserSchema>;Automatic semantic inference
snaptype inspects the actual values in your JSON — not just the types. Email addresses become z.email(), ISO dates become z.iso.datetime(), URLs become z.url() — automatically. String fields with few distinct values are promoted to union literals (enum detection).
Options
| Flag | Description |
|---|---|
--name <name> | Override the root type name (default: derived from filename) |
--zod | Generate a Zod schema instead of (or alongside) TypeScript interfaces |
--ts | Also 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 |
--readonly | Add readonly to all properties |
--emit <kind> | Declaration style: interface (default) or type |
-c, --config <path> | Path to .snaptyperc config file |
Pro features Pro
Multi-file merging
Pass multiple JSON files and snaptype merges them into a single unified type. Fields that appear in some files but not all become optional — giving you the most accurate type when a real-world API returns slightly different shapes.
snaptype from-json sample1.json sample2.json sample3.json -o src/types/user.tsWatch mode
Automatically regenerate types whenever the source file changes. Useful during development when your JSON schema is still evolving.
snaptype from-json users.json --watch -o src/types/user.ts