About the JSON to TypeScript
Paste a JSON sample to get ready-to-use TypeScript interfaces. Every nested object gets its own named type, arrays are typed by their contents, and fields missing from some items are marked optional.
Key features
- Interfaces or type aliases, exported and ready to import
- A separate named type for every nested object
- Optional (?) fields detected across array items
- Nullable values typed as T | null
- Keys like "first-name" quoted correctly
- Optional readonly modifiers
Stop writing API types by hand
Typing an API response field by field is slow and error-prone, and it drifts out of date. Paste a real response and get accurate TypeScript types in a second, then import them across your front end or Node.js code for autocomplete and compile-time checks.
Example
{ "id": 7, "name": "Ada", "tags": ["admin"], "address": { "city": "London" }, "manager": null }
export interface Root {
id: number;
name: string;
tags: string[];
address: Address;
manager: unknown;
}
export interface Address {
city: string;
}
Type mapping
| JSON | TypeScript |
|---|---|
"text" | string |
42, 3.14 | number |
true | boolean |
[...] | T[] |
{...} | named interface |
null alongside a value | T | null |
| missing in some items | field?: T |
| unknown / mixed | unknown |
Using the types
import type { Root } from "./types";
const res = await fetch("/api/user");
const user = (await res.json()) as Root;
Tips
- Name the root after your data (e.g.
User,Order) with the Root name option. - Use several array items so optional fields are detected.
- Turn on readonly for immutable data, such as Redux or React state.
Related tools
Using plain JavaScript? Try JSON to JavaScript for JSDoc types. Need runtime models in other languages? See JSON to Python, JSON to C# and JSON to Go.
How to use the JSON to TypeScript
- Paste your data into the input box, upload a file, or click Sample to try it out.
- Adjust the options above the editor if you need to (indentation, delimiter, and so on).
- The TypeScript appears instantly in the output panel. Any errors are shown with their line and column.
- Click Copy to copy the result, or Download to save it as a file.