Generate TypeScript types from JSON data
An AI prompt to generate TypeScript types from JSON — interfaces, optional fields, unions, enums and a runtime validation schema if needed.
- Published
- Works with
- ChatGPT, Claude and Gemini
- Level
- Intermediate
Writing TypeScript types by hand for a large API response is slow and easy to get wrong, especially with nullable fields, mixed arrays and nested objects. This prompt turns sample JSON into clean TypeScript types, points out fields it can't be sure about and can add a runtime validation schema so bad data fails loudly.
The prompt
Act as a senior TypeScript developer. Generate types from this JSON.
Sample JSON (one or more responses):
What the data represents and where it comes from: [CONTEXT, e.g. REST API order endpoint]
Naming style: [NAMING, e.g. PascalCase interfaces, camelCase fields]
Validation library, if any: [LIBRARY, e.g. Zod, none]
Produce:
1. TypeScript interfaces or types for every object, with nested types named clearly.
2. Optional and nullable fields marked correctly; explain which ones you inferred from comparing samples.
3. String literal unions or enums where values look like a fixed set (and say they are a guess).
4. Date strings, IDs and money fields noted with comments.
5. If a library is given, a matching validation schema and the inferred type from it.
6. A list of assumptions I should confirm against the API documentation.
Return code in one block, then the assumptions list.
How to use it
- Paste several real responses, including edge cases, so optional fields are detected.
- Confirm the assumptions against the API docs before relying on unions.
- Validate at the boundary where data enters your app if you added a schema.
Tips
- Document the endpoint afterwards with the API documentation from code prompt.
- Remove secrets and personal data from samples before pasting them.
Frequently asked questions
What does this prompt do?
An AI prompt to generate TypeScript types from JSON — interfaces, optional fields, unions, enums and a runtime validation schema if needed.
Which AI tools does it work with?
It is written for ChatGPT, Claude and Gemini. It also works in other AI tools that accept text prompts, with small changes.
What do I need to fill in?
1 blank: json samples. Click each blue blank on the page, type your details and press Copy prompt.
Is this prompt free to use?
Yes. You can copy and use it for personal and commercial work.