quicktype's type inference improves dramatically when you provide multiple JSON samples. This post explains why and how to use this feature.
The Problem with Single Samples
A single JSON sample might not represent all possible values:
// Sample 1: User is online
{
"name": "Alice",
"status": "online",
"lastSeen": null
}
From this, quicktype infers lastSeen is always null. But in reality:
// Sample 2: User is offline
{
"name": "Bob",
"status": "offline",
"lastSeen": "2018-03-08T10:30:00Z"
}
Multiple Samples to the Rescue
Give quicktype both samples, and it correctly infers:
interface User {
name: string;
status: string;
lastSeen: string | null;
}
Using Multiple Samples
Command Line
quicktype sample1.json sample2.json sample3.json -o Types.ts
Web App
- Go to app.quicktype.io
- Paste your first JSON sample
- Click "Add sample" (+ button)
- Paste additional samples
Best Practices
- Include edge cases: Empty arrays, null values, optional fields
- Cover all variants: Different enum values, union types
- Use real data: Production API responses are ideal
How It Works
quicktype merges the type graphs from all samples, creating unions where values differ. This produces more robust types that handle real-world data variations.