Pasting a JSON response into a converter and getting a TypeScript interface back feels like magic, and for the first ten minutes it is. Then the API returns a null where your type said string, or omits a field your type said was always there, and the compiler, which trusted the interface, said nothing.
The converter did what it was asked. The trouble is what it was asked: to describe the shape of one sample, which is not the same as the type of the data. This article walks through where the two differ, using real output from a converter, so you know which fields to fix by hand.
What a converter can and cannot know
A converter sees values, not rules. From "name": "Ada" it can tell you that this field was a string in this sample. It cannot tell you:
- whether the field is ever missing,
- whether it is ever
null, - whether it is always one of three specific strings,
- what an empty array would have contained.
Everything below follows from that. The examples use the JSON to TypeScript converter on this site, set to produce an interface.
One sample: null, empty arrays and literal-looking strings
Take a single user object from an API:
{"id":1,"name":"Ada","avatar":null,"tags":[],"status":"active","score":9.5}
The generated interface is:
export interface User {
id: number;
name: string;
avatar: null;
tags: unknown[];
status: string;
score: number;
}
Three of those lines are almost certainly wrong, and each is wrong in a different way:
avatar: null. The sample had no avatar, so the only type the converter saw wasnull. In real data it is probablystring | null. As written, TypeScript will reject any code that treats it as a string.tags: unknown[]. The array was empty, so there was nothing to infer an element type from. You need to writestring[]yourself.status: string. It was"active"in the sample. The real type may be"active" | "inactive" | "banned", and a plainstringlets a typo such as"actve"through without complaint.
More samples help, up to a point
A list with several items gives the converter more to work with. Given two users where the second has no email:
[{"id":1,"name":"Ada","email":"a@x.io"},{"id":2,"name":"Bob"}]
it merges them correctly:
export interface User {
id: number;
name: string;
email?: string;
}
export type Users = User[];
email is optional because one element lacked it. The same merging catches nullable fields, if the sample happens to include one of each:
[{"id":1,"avatar":null},{"id":2,"avatar":"b.png"}]
export interface User {
id: number;
avatar: null | string;
}
This is the best advice for getting a good result from any converter: give it several real examples, including the awkward ones, rather than one tidy one. A field that is missing or null in some record is only discoverable if such a record is in the sample.
It is still a sample. If no record in your sample has a missing email, no converter can know the field is optional.
Keys that are not identifiers
JSON keys can be any string; TypeScript property names are friendlier but still allow any string when quoted. A sensible converter quotes the keys that need it:
export interface User {
"first-name": string;
"2nd": boolean;
}
That is valid and usable, but it is a hint that the API's naming does not match your code's. A mapping layer that converts first-name to firstName at the boundary is often worth writing, so the awkward names stay in one place.
Optional is not the same as undefined
A detail that causes confusing bugs: email?: string and email: string | undefined are not interchangeable. The first says the key may be absent. The second says the key is always present, though its value may be undefined. JSON has no undefined, so a field that arrives as null or is missing needs | null or ?, not | undefined.
If you turn on the exactOptionalPropertyTypes compiler option, the difference becomes stricter still: an optional property then cannot be explicitly set to undefined.
A checklist for reviewing generated types
After generating an interface, go through it line by line:
- Every
nulltype. Is it really only evernull? It is probablyT | null. - Every
unknown[]. Replace it with the real element type. - Every
stringthat is really a fixed set. Statuses, roles and modes deserve a union of literals. - Every required field. Could it be absent in some responses? If so, add
?. - Numbers. A
numberis fine for JSON, but IDs beyond 2^53 lose precision in JavaScript, so large IDs are safer as strings. - Dates. JSON has no date type. A field that holds an ISO date arrives as a
string, and it stays astringuntil you parse it.
Better sources than a sample
If you can avoid inferring types from a sample at all, do. When the API publishes a JSON Schema or an OpenAPI description, generate types from that. It states nullability, optionality and allowed values outright, so nothing is guessed. Tools such as json-schema-to-typescript and openapi-typescript do this. The syntax versus schema validation post explains what a schema adds over a valid-looking document.
Types describe what the compiler believes, not what the network delivers. For data from outside your code, a runtime validator such as Zod, which can also derive the static type, checks the response when it arrives and turns a surprise into a clear error at the boundary.
The short version
A generated interface is a draft. Use it to save the typing, give the converter several honest examples, and then read every line asking "could this be missing, null or one of several values?" The converter cannot answer that question. Only the real data, or a schema, can.
