DevForge

JSON to TypeScript

The two things hand-written interfaces usually get wrong are the same two things: marking a property optional when it is really nullable, and marking it nullable when it is really optional. They mean different things to the compiler. Given several samples, this infers each one separately and writes them correctly.

json
typescript
options
target

A single object cannot reveal which keys are optional. Paste an array of several samples to detect them.

how to use it

  1. 01Paste a JSON object, an array of samples, or NDJSON.
  2. 02Name the root interface.
  3. 03Copy the interfaces into your project.

questions

Why does one field use ? and another use | null?
A question mark means the key may be absent. A null union means the key is present with a null value. Only one of those is satisfied by leaving the property out.
Are nested objects extracted?
Yes, each becomes its own named interface, declared before whatever uses it, and identical shapes share a single definition.
What about keys that are not valid identifiers?
They are quoted, so a key like content-type is written as a string literal rather than producing invalid syntax.

related