Why generate types from JSON
When you consume an API or load a config file, the JSON arrives untyped: to TypeScript it is just a generic object. Writing the interfaces that describe that shape by hand is tedious and error-prone, especially with nested objects and arrays. Generating them automatically gives you autocompletion, type checking, and living documentation of the data structure, without typing out every single field.
How types are inferred
The tool walks the JSON and, for each value, picks the most specific TypeScript type it can: text becomes string, numbers number, booleans boolean, and null stays as null. Each nested object becomes its own interface with a PascalCase name derived from the key, and arrays are typed by looking at their first element: an array of strings is string[], an array of objects is ObjectName[].
Optional versus required fields
A sample JSON has all of its fields present, so the tool generates them as required. But in the real world many APIs return fields that are sometimes missing. If you know a field can be absent, mark it optional by adding ? before the colon: phone?: string. Likewise, if a field can be null in addition to its type, write a union such as nickname: string | null.
Unions and mixed types
Arrays are not always homogeneous. If an array mixes numbers and strings, generating number[] would be wrong. In that case the tool builds a union that reflects every type present, for example (number | string)[]. For an empty array there is nothing to infer from, so any[] is used and it is up to you to refine it. Unions are also handy by hand when the same field can hold values of different types.
Limits of inference
Inferring types from a single example has clear boundaries. The tool cannot know that a number is really an enum of three values, that a string follows an ISO date format, or tell a tuple apart from a regular array. It also cannot detect optional fields if your example always includes them. Treat the result as a solid starting point and refine it: rename generic interfaces, add optionals, and replace any where you have more context than the example provides.
Reference: JSON value to TypeScript type
| Value in the JSON | TypeScript type |
|---|---|
"text" | string |
42 / 3.14 | number |
true / false | boolean |
null | null |
{ ... } | named interface (PascalCase) |
["x","y"] | string[] |
[] | any[] |
[1,"a"] | (number | string)[] |