Skip to main content
AIPromptIndex
GitHub Copilot Coding intermediate

TypeScript Type Generator from JSON

Added Apr 2, 2026

Generate comprehensive TypeScript types and interfaces from the following JSON data: [JSON_DATA]. The types are for [TYPE_CONTEXT]. Apply these rules: 1) Create named interfaces for all object shapes — never use inline anonymous types for objects that appear more than once. 2) Use string literal unions instead of plain 'string' for fields with a known set of values (detect these from the data patterns). 3) Mark fields as optional (?) only if they are genuinely absent in some records — analyze all provided examples. 4) Add JSDoc comments to every interface and every non-obvious field explaining its purpose and constraints. 5) Generate utility types: a creation input type (omitting auto-generated fields like id, createdAt), an update input type (making all fields optional with Partial), and a list response type with pagination metadata. 6) Include branded types for IDs to prevent mixing different entity IDs (e.g., UserId vs OrderId). 7) Generate Zod schemas that mirror the TypeScript types for runtime validation. 8) Export a type guard function for each interface. 9) If the JSON contains nested objects or arrays, create separate named types for each level. 10) Use [NAMING_CONVENTION] for all type names. Flag any inconsistencies in the JSON data that suggest potential bugs.
0
Share
Try in GitHub Copilot

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;
}
```
typescript types zod type-safety json

Get the Best AI Prompts Weekly

Curated prompts, tips, and guides delivered to your inbox every week. Free.