How this tool works
A JSON sample is walked and a TypeScript interface is inferred from what is actually there. Nested objects become their own named types rather than being inlined, so the result reads like something a person would have written. Arrays are examined across all their elements rather than only the first, which is what catches a field that is a string in most records and null in one.
Where the elements of an array disagree, a union type is produced instead of a guess. Keys that are not valid identifiers are quoted, and keys colliding with reserved words are handled. The optional-properties setting marks a field as optional where it is missing from some objects in an array — which is usually closer to the truth than declaring it required and finding out later.
What can and cannot be inferred from a sample
- A sample is evidence, not a specification: a field that happens to be a string in your sample is typed as a string. If the API can also return null there, only the API's documentation will tell you.
- Feed it several records rather than one: an array of objects gives the inference something to compare, which is how optional fields and unions are discovered at all.
- Numbers lose their distinctions: JSON has one number type, so an integer identifier and a decimal price both become number. Narrowing those is a manual job.
- Dates arrive as strings: JSON has no date type. A field that looks like a timestamp is typed as a string, and converting it is a decision for your code.