🧩

JSON to TypeScript

Paste JSON and get TypeScript interfaces, including nested objects and arrays

Note: The types are inferred from this JSON, not a JSON Schema. When array elements have inconsistent structures, a union type is generated, and field names that are not valid identifiers are quoted.

Instructions

JSON 转 TypeScript 接口使用说明



功能简介


拿到一份接口返回的 JSON,要手写 TypeScript 类型很费时间。这个工具直接读 JSON 结构,递归推断出嵌套的 interface。

使用方法


  • 把 JSON 粘贴到输入框,点「填入示例」可以先看一个复杂例子

  • 填好根接口名(默认 Root),选 `interface` 或 `type` 别名

  • 如果字段可能缺失,勾上「字段可选」给每个字段加 `?`

  • 点「生成 TypeScript」,结果按引用顺序输出,子类型在前、根类型在后

  • 点「复制结果」复制走


  • 注意事项


  • 类型是从这一份 JSON 推断出来的:字段为 `null` 时只会得到 `null`,缺字段也看不出来。要更准确请用多份样本或手写补充

  • 数组里元素结构不一致时会生成联合类型,例如 `(A | B)[]`

  • 空数组只能推断出 `unknown[]`,需要你自己补类型

  • 字段名不是合法标识符时会用引号包起来,符合 TypeScript 语法
  • ❓ FAQ

    What if the object types in an array are inconsistent?

    The tool generates one interface for each distinct structure and writes the array element type as a union, for example `(User | Admin)[]`. You can narrow it down after generation based on your actual business logic.

    Why is the type of a null field null?

    Because there is only this one piece of data, it is impossible to tell whether it will be a string or a number later. It is recommended to add all the types that have appeared in real data, or manually change it to `string | null`.

    Does it support generating Go or Java types?

    Currently it only generates TypeScript. If you want to see the structure first, the nested interface names inferred by this tool are not hard to rewrite as structs in other languages.

    📝 Related Articles

    tool-tutorials

    How to Use a Currency Converter: A Three-Step Operation Process for Beginners

    This article provides executable troubleshooting steps in the order of "environment—input—data," helping you quickly locate issues such as a currency converter not responding, abnormal results, and mobile freezing, and explains the differences between web tools and the system's built-in conversion, so you can solve it yourself without repeatedly going back to the search results page.

    tool-tutorials

    How to Convert JSON to CSV: Complete Beginner Steps

    Paste JSON of any level into the browser and convert it to CSV. This article provides complete steps for judging the data structure, handling nested fields, troubleshooting conversion failures, dealing with large files, and API debugging, so you can get a usable spreadsheet file without repeated trial and error.

    tool-tutorials

    How to Deduplicate and Sort Text Without Changing the Original Order

    Use a locally running deduplication tool to process text, keep the first occurrence position, delete duplicates without disrupting the original line order, and also understand the precautions for large file lag, mobile use, and API debugging scenarios, so you can get clean, ready-to-use results in just a few minutes.

    tool-tutorials

    How to Use a Countdown Timer: Complete Steps from Setting to Ringing

    This guide breaks down how to use a countdown timer into actionable steps, covering duration setting, start/pause, ring troubleshooting, phone mute and background restrictions, maximum duration limits, and the trade-offs between online and system timers, with developer API debugging points included—read it and you can start using it right away.