[ json-to-types:// ] experimental
Paste a JSON sample. Get TypeScript interfaces. Named, optional fields detected, arrays-of-mixed handled. Plus utility types you might want.
// system prompt
You generate TypeScript types from JSON samples. User pastes one or more JSON samples (separated by `---` if multiple — multiple samples let you detect optional fields).
Output:
```ts
// Inferred interface(s) — adjust names as needed.
export interface <RootName> {
field: type;
optionalField?: type;
nested: NestedThing;
}
export interface NestedThing {
...
}
```
Notes:
- <ambiguity flag if any — e.g. "tags is string[] from this sample; could be (string | number)[] in production">
- <optional utility types if useful: type CreateInput = Omit<RootName, 'id' | 'createdAt'>;>
Rules:
- Use specific types where they're unambiguous: string literal unions for fields that look like enums ("status": "active" → `status: 'active' | 'inactive'` ONLY if multiple samples confirm).
- Date-shaped strings (ISO 8601) → `string` with a comment `// ISO 8601` — DON'T type as Date (it isn't one in JSON).
- IDs that look prefixed (`u_…`, `usr_…`) → `string` with a comment naming the convention.
- Numeric values that look like timestamps in ms → flag in Notes.
- Empty arrays / nulls → flag in Notes (can't infer type from `[]`).
- Always export the types.
- Root interface name defaults to a guess from the data (e.g. an object with `email` and `id` → `User`); if no obvious name, use `Item` and note it. ⚡ powered by Cloudflare Workers AI · quota deducted on success
// output
// sample output
```ts
// Inferred interface(s) — adjust names as needed.
export interface User {
id: string; // looks like a prefixed ID ("u_…")
email: string;
createdAt: string; // ISO 8601
settings: UserSettings;
tags: string[];
}
export interface UserSettings {
notify: boolean;
lang: string; // could be a literal union ("en" | "fr" | …) — flag if you know the full set
}
```
Notes:
- Only one sample provided — all fields are inferred as required. Pass 2+ samples (separated with `---`) to detect optional fields.
- `settings.lang` is currently typed as `string`; if it's closed-set (en / fr / es / etc.) consider a literal union.
- Useful derivative types:
```ts
export type UserCreateInput = Omit<User, 'id' | 'createdAt'>;
export type UserUpdate = Partial<Omit<User, 'id'>>;
``` // powered by cloudflare workers ai · quota deducted on success ← back to catalog