TypeScript Type Generator from JSON
Added Apr 2, 2026
About This Prompt
This prompt converts raw JSON data into a complete TypeScript type system with runtime validation, going far beyond simple JSON-to-TypeScript converters. The branded ID types prevent the class of bugs where you accidentally pass a user ID where an order ID is expected. The Zod schema generation means you get both compile-time and runtime type safety from a single source of truth. The automatic detection of string literal unions and optional fields means the types are as precise as possible rather than falling back to loose string and optional-everything patterns. The included type guards let you narrow types safely in conditional logic. Essential for teams adopting TypeScript in existing JavaScript projects or integrating with untyped external APIs.
Variables to Customize
[JSON_DATA]
The JSON data to generate types from
Example: {"id": "usr_abc123", "name": "Jane Doe", "email": "[email protected]", "role": "admin", "plan": "pro", "createdAt": "2026-01-15T10:30:00Z"}
[TYPE_CONTEXT]
Where these types will be used
Example: a multi-tenant SaaS user management system
[NAMING_CONVENTION]
How to name the generated types
Example: PascalCase with entity prefix (e.g., UserProfile, UserRole, UserCreateInput)
Tips for Best Results
- Provide multiple JSON examples of the same entity to help Copilot detect truly optional fields vs always-present ones
- Include edge cases in your sample data — null values, empty arrays, missing fields — for more accurate types
- Paste your existing types file so Copilot can extend rather than duplicate what you already have
Example Output
```typescript
import { z } from 'zod';
// --- Branded ID Types ---
type UserId = string & { readonly __brand: 'UserId' };
/** A user account in the system */
interface User {
/** Unique user identifier (prefixed with usr_) */
id: UserId;
/** User's display name */
name: string;
/** User's email address — unique across the system */
email: string;
/** User's permission level */
role: UserRole;
/** Subscription tier */
plan: UserPlan;
/** ISO 8601 timestamp of account creation */
createdAt: string;
}
type UserRole = 'admin' | 'member' | 'viewer';
type UserPlan = 'free' | 'pro' | 'enterprise';
/** Input for creating a new user (server generates id and createdAt) */
type UserCreateInput = Omit<User, 'id' | 'createdAt'>;
/** Input for updating a user (all fields optional) */
type UserUpdateInput = Partial<UserCreateInput>;
// --- Zod Schemas ---
const UserSchema = z.object({
id: z.string().startsWith('usr_'),
name: z.string().min(1),
email: z.string().email(),
role: z.enum(['admin', 'member', 'viewer']),
plan: z.enum(['free', 'pro', 'enterprise']),
createdAt: z.string().datetime(),
});
// --- Type Guard ---
function isUser(value: unknown): value is User {
return UserSchema.safeParse(value).success;
}
```