How it works
The converter walks the parsed JSON and builds a shape for every value: string, number, boolean, null, an array of something, or an object with fields. Each nested object gets its own named type, called after its key in PascalCase, so address becomes Address. Array items get the singular form: posts gives Post, categories gives Category.
Arrays are read item by item
One sample object tells you little about an API. An array of them tells you more, so all items are merged into one type:
- A key missing from some items becomes optional. If only one post has
coverUrl, you getcoverUrl?: string. - Different kinds under one key become a union.
code: 503in one item andcode: "ETIMEDOUT"in another givecode: number | string. nullis kept as| null, not turned into optional. A field that is present andnullis not the same as a field that isn't there, andJSON.stringifykeeps the first but drops the second.- Empty arrays are
unknown[]. There is nothing to infer from. Paste a sample where the array has items, or fix the type by hand.
Objects with exactly the same fields share one type. In the Config preset, primary and replica are both Primary. Rename it to something like DatabaseHost once you paste it into your code.
interface or type?
For plain object shapes they behave the same, so pick what your codebase uses. The practical differences: interfaces with the same name merge (declaration merging), and only type can name a union or a primitive. That's why a root that is an array is always written as type Root = Item[].
Types vs a Zod schema
A type disappears when TypeScript compiles. Writing const user = (await res.json()) as User is a promise to the compiler, and nothing checks it at runtime. If the API changes, the code still compiles and breaks somewhere later, far from the cause.
A Zod schema is a runtime check. z.infer derives the type from it, so there's one source of truth:
import { z } from 'zod';
const UserSchema = z.object({
id: z.number(),
email: z.string(),
avatarUrl: z.string().nullable(),
});
type User = z.infer<typeof UserSchema>;
const result = UserSchema.safeParse(await res.json());
if (!result.success) {
console.error(result.error.issues);
} else {
result.data.email; // typed as string
}Use parse when bad data should throw, and safeParse when you want to handle it. Schemas are emitted in dependency order, since a const can't be used before it's declared. The output works with Zod 3 and Zod 4.
What a sample can't tell you
The result describes the JSON you pasted, not every response the API can return. Tighten it by hand where you know more:
- A
stringthat is always one of a few values should be a union of literals ('admin' | 'editor') orz.enum([...]). - Dates arrive as strings. In Zod,
z.coerce.date()turns them intoDateobjects. - A number that must be whole can get
.int(), an email fieldz.email()in Zod 4 orz.string().email()in Zod 3. - An object used as a dictionary (
{ "en": ..., "de": ... }) is better typed asRecord<string, T>than as a fixed set of keys. Empty objects are already written that way.