Coding Prompts
TypeScript Types from JSON
Generate TypeScript interfaces or types from any JSON object or API response.
Prompt
Generate TypeScript interfaces from the following JSON sample. **JSON:** ```json [JSON_SAMPLE] ``` Requirements: 1. **Prefer `interface` over `type`** unless a union or mapped type is more appropriate 2. **Optional fields** — use the `?` suffix for any field that might be absent or `null` in real data (use your judgment based on the field name and value) 3. **Union types** — if a field could reasonably be more than one type (e.g., `string | null`, `number | string`), use a union 4. **Nested objects** — define a separate named interface for each nested object; do not use inline object types 5. **Arrays** — type array elements correctly (e.g., `User[]`, not `any[]`) 6. **JSDoc comments** — add a brief `/** ... */` comment on any field whose purpose is not obvious from its name 7. **Top-level type** — the outermost interface should be named based on what the JSON represents (e.g., `ApiResponse`, `UserProfile`, `OrderSummary`) Output only the TypeScript code — no explanation needed unless a field's type was ambiguous, in which case add a short inline comment.
How to Use
Paste any JSON — an API response you copied from a browser DevTools Network tab, a sample payload from documentation, or a fixture file from your codebase. You'll get a set of TypeScript interfaces that you can drop directly into your project. Review the optional field markings (?) and adjust any that you know are always present or always absent in your real data.
Variables
| Variable | Description |
|---|---|
| [JSON_SAMPLE] | A representative JSON object or array. It should include all possible fields — if some fields only appear in certain conditions, include a sample that shows them so the model can mark them as optional. Paste raw JSON, not a JavaScript object literal. |
Tips
- If your API returns different shapes depending on status (e.g.,
{ data: User }on success and{ error: string }on failure), paste both samples and ask for a discriminated union — the model will generate a pattern liketype ApiResult = SuccessResponse | ErrorResponse. - After generating the types, paste them back in a follow-up and ask the model to write a Zod schema or io-ts codec from them — you get runtime validation for free.