Every time you integrate a new API endpoint, you face the same tedious task: writing TypeScript interfaces for the response. The Dev Cosmos JSON→TypeScript tool eliminates this entirely. Paste JSON, click generate, and get production-ready interfaces in one second.
interface vs type alias
Both interface and type can describe object shapes, but they have important differences:
| Feature | interface | type |
|---|---|---|
| Extension | extends keyword | Intersection & |
| Declaration merging | Yes (multiple declarations merge) | No |
| Union types | No | Yes |
| Mapped types | No | Yes |
| Best for | Public API shapes, OOP | Computed types, unions |
interface for object shapes that can be extended or merged. Use type when you need union types, intersection types, or mapped types.Optional Fields (?)
When enabled, all generated properties include the ? modifier, making them optional. This reflects real-world API responses which may omit fields:
// Optional — field may or may not be present
interface User {
id?: number;
name?: string;
email?: string;
}
// Required — field must always be present
interface User {
id: number;
name: string;
email: string;
}
Use optional for data coming from APIs you don't control. Use required for data your own system writes.
Readonly Properties
The readonly modifier prevents reassignment after construction. It's ideal for immutable DTOs:
interface OrderResponse {
readonly orderId: string; // Cannot be reassigned
readonly total: number;
items: OrderItem[]; // Array reference is mutable
}
Note that readonly on an array property prevents reassignment of the array reference, but not mutation of the array contents. For deeply immutable arrays use readonly Item[].
Nested Objects and Arrays
The generator recursively handles nested objects and arrays:
// JSON input
{ "order": { "id": 1, "items": [{ "sku": "X", "qty": 2 }] } }
// Generated TypeScript
export interface RootObject {
order?: Order;
}
export interface Order {
id?: number;
items?: Items[];
}
export interface Items {
sku?: string;
qty?: number;
}
After generating, rename generic names like Items to be more descriptive: OrderLineItem.
Recommended Workflow
- Copy a response body from your browser's Network tab or Postman
- Paste into the JSON→TypeScript tool
- Set the root interface name to match the response (e.g.
UserResponse) - Enable Optional fields for external APIs, disable for internal ones
- Enable Export so types are usable across modules
- Copy output to a
types/api.tsfile in your project - Rename any generic child interface names