Skip to main content
Cloud & AI Hub
Browse
Glossary AI Directory Playgrounds Models Prompts Explainers Strategy Matrix Benchmark Decoder

TypeScript Interface Compiler

Use Case: Extract strongly-typed TypeScript interfaces and types from API JSON responses

System Instructions

You are a TypeScript architect. Convert JSON API responses into idiomatic, well-documented TypeScript interfaces. Output only valid TypeScript.

User Prompt Template

Generate TypeScript interfaces for this API response:

{JSON_RESPONSE}

API context: {API_CONTEXT}

Run This Prompt โ€” SDK Snippets

Implementation Guidelines

What This Prompt Does

This prompt transforms raw API JSON responses into a complete set of TypeScript interfaces, handling nested objects, optional fields, union types, and ISO date strings automatically. Instead of relying on error-prone manual type authoring, it produces interfaces that accurately reflect the runtime shape of the data, complete with JSDoc comments derived from field names and values.

System Prompt

You are a TypeScript 5.x architect specializing in API type generation and type safety.

When converting JSON to TypeScript interfaces:
1. Create a named interface for the root object and every nested object.
2. Name interfaces using PascalCase derived from their context (e.g., a "user" field becomes 
   a "User" interface; an "address" field within it becomes "Address").
3. Handle optional fields: if a field's value is null in the example, mark it "field?: Type | null".
4. For string fields that look like ISO 8601 dates, type them as "string" with a JSDoc 
   @format date-time comment โ€” do NOT use the Date type (APIs return strings, not objects).
5. For arrays, generate "Item[]" types with a named interface for array elements.
6. Add a JSDoc comment to every interface and every property.
7. Export all interfaces.
8. If the JSON implies a discriminated union (e.g., objects with a "type" or "kind" field), 
   model it as a TypeScript discriminated union.
9. Output ONLY valid TypeScript โ€” no markdown fences, no prose.

User Prompt Template

Generate TypeScript interfaces for the following JSON API response.

JSON Response:
{JSON_RESPONSE}

API Context:
- Endpoint: {ENDPOINT} (e.g., "GET /api/v2/users/:id")
- Description: {API_CONTEXT}
- Nullability notes: {NULLABILITY_NOTES} (e.g., "bio can be null, phone is always present")

TypeScript interfaces:

Example Output

Input {JSON_RESPONSE}:

{
  "user": {
    "id": "usr_abc123",
    "email": "[email protected]",
    "full_name": "Alice Smith",
    "avatar_url": "https://cdn.example.com/avatars/alice.png",
    "role": "admin",
    "created_at": "2024-01-15T10:30:00Z",
    "subscription": {
      "plan": "pro",
      "expires_at": "2025-01-15T10:30:00Z",
      "seats": 5
    },
    "tags": ["early-adopter", "beta-tester"]
  }
}

Model output:

/** The subscription plan tier for a user account. */
export type SubscriptionPlan = "free" | "pro" | "enterprise";

/** Subscription details attached to a user account. */
export interface Subscription {
  /** The active plan name. */
  plan: SubscriptionPlan;
  /** @format date-time ISO 8601 timestamp when the subscription expires. */
  expires_at: string;
  /** Number of seats included in this subscription. */
  seats: number;
}

/** A registered user account. */
export interface User {
  /** Unique user identifier with "usr_" prefix. */
  id: string;
  /** User's primary email address. */
  email: string;
  /** User's display name. */
  full_name: string;
  /** URL of the user's avatar image, or null if not set. */
  avatar_url: string | null;
  /** User's access control role. */
  role: "admin" | "member" | "viewer";
  /** @format date-time ISO 8601 timestamp of account creation. */
  created_at: string;
  /** Active subscription details. */
  subscription: Subscription;
  /** List of feature tags associated with the user. */
  tags: string[];
}

/** Response shape for GET /api/v2/users/:id */
export interface GetUserResponse {
  user: User;
}

Tips & Variations

  • Zod schema generation: Add "Also generate a Zod schema that mirrors these interfaces for runtime validation" to your user prompt. The model will produce z.object({...}) schemas that can be used with safeParse for API response validation.
  • Handle large responses: For deeply nested payloads (10+ levels), feed the model the flattened list of unique nested object structures separately. This prevents hallucination on deeply recursive types.
  • GraphQL variant: Replace the system prompt objective with "Generate a GraphQL fragment definition" to produce fragment UserFields on User { ... } SDL from the same JSON input.
  • Utility types: Append "Also generate Partial<T>, Pick<T>, and CreateRequest types for POST /api/v2/users" to have the model derive utility types automatically for CRUD operations.