Skip to content

Nathmaxx/snaptype

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

snaptype

Stop writing TypeScript types by hand.

snaptype is a CLI that turns your JSON files, CSV exports, REST APIs, and OpenAPI/GraphQL schemas into TypeScript interfaces and Zod schemas — in one command.

snaptype.dev — docs, Pro licence, live demo


The problem

Every time you hit an API or open a data file, you end up writing types by hand. You copy-paste a JSON response, squint at the shape, type out an interface. The API changes — you do it again.

snaptype automates that step entirely.


Quick start

npx snaptype from-json ./user.json -o types.ts

Input — user.json:

{
  "id": 1,
  "name": "Alice",
  "email": "alice@example.com",
  "role": "admin",
  "createdAt": "2024-01-15T10:30:00Z",
  "address": { "city": "Paris", "zip": "75001" }
}

Output — types.ts:

// Generated by snaptype — do not edit manually

export interface Address {
  city: string;
  zip: string;
}

export interface User {
  id: number;
  name: string;
  email: string; // email
  role: string;
  createdAt: string; // ISO 8601
  address: Address;
}

Add --zod to also get a ready-to-use Zod schema:

npx snaptype from-json ./user.json -o types.ts --zod
// types.zod.ts — Generated by snaptype

import { z } from 'zod';

export const AddressSchema = z.object({
  city: z.string(),
  zip: z.string(),
});

export const UserSchema = z.object({
  id: z.number(),
  name: z.string(),
  email: z.string().email(),
  role: z.string(),
  createdAt: z.string().datetime(),
  address: AddressSchema,
});

export type User = z.infer<typeof UserSchema>;

Installation

npm install -D snaptype
# peer dep if you use --zod
npm install zod

Requires Node.js 20+.


Input sources

Source Command
Local JSON file snaptype from-json ./data.json -o types.ts
Live API / URL snaptype from-url https://api.example.com/users -o types.ts
CSV file snaptype from-csv ./export.csv -o types.ts
stdin (pipe) curl https://… | snaptype from-stdin --name User -o types.ts
OpenAPI 3.x spec snaptype from-openapi ./openapi.yaml -o types.ts
GraphQL endpoint snaptype from-graphql https://…/graphql -o types.ts (Pro)

What it infers automatically

  • Primitives: string, number, boolean, null
  • Nested objects and arrays
  • Optional and nullable fields
  • Semantic hints on string fields — email, url, ISO 8601 dates → .email(), .url(), .datetime() in Zod
  • Enum / union literals when a field has low cardinality (≤ 10 distinct values, ≥ 80% coverage)
  • $ref, oneOf, allOf, anyOf from OpenAPI specs
  • GraphQL scalars, enums, unions, and interfaces

More commands

# Detect breaking changes between two type files (Pro)
snaptype diff old.ts new.ts

# Generate realistic mock data from a schema (Pro)
snaptype mock ./types.ts -o mocks.ts

# Convert existing TypeScript interfaces to Zod schemas (Pro)
snaptype to-zod ./src/types/user.ts

# Re-export all generated files in one barrel file
snaptype barrel ./src/types

# Watch a source file and regenerate on change (Pro)
snaptype from-json ./api.json -o types.ts --watch

Project config

Drop a .snaptyperc at the root of your project to stop repeating flags:

{
  "naming": "camel",
  "emit": "interface",
  "zod": true,
  "outDir": "src/types"
}

CLI flags always take priority. See the configuration docs for all keys.


Free vs Pro

Pro is a one-time purchase — no subscription, works across machines.

Feature Free Pro
from-json (single file)
from-url, from-csv, from-stdin
from-openapi (single file)
TypeScript + Zod generation
Semantic inference (email, date, url)
Enum / union literal detection
--readonly, .snaptyperc, barrel
from-json / from-url (multiple files)
from-graphql
snaptype diff + --ci
snaptype mock
snaptype to-zod
--watch

Get Pro at snaptype.dev


License

MIT — free tier is free forever. Pro features require a licence key.

About

Generate TypeScript types and Zod schemas from JSON, CSV, or live API responses

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors