Monu Tools

JSON 转 TypeScript

从 JSON 示例生成 TypeScript 接口,包含嵌套类型、数组和可选字段。在你的浏览器中运行。

如何使用 JSON 转 TS

  1. 01

    粘贴一个 JSON 对象或数组。

  2. 02

    可选择设置根接口名称。

  3. 03

    复制生成的 TypeScript 接口。

What the generator does

This is a JSON to TypeScript generator that infers a set of interfaces from an example JSON object or array, so you can paste an API response and get ready-to-use types instead of writing them by hand.

It is a big time-saver when you are consuming an API or a config file: paste a response, optionally name the root interface, and copy clean TypeScript straight into your code.

Generation runs entirely in your browser, so your data never leaves your device, which matters when the sample contains real values.

How it handles real-world shapes

It handles real-world shapes: nested objects become their own named interfaces referenced from the parent, arrays of objects are merged into one interface, and union types are produced where values vary.

Optional fields across mixed arrays

When the objects in an array do not all have the same keys, the keys missing from some elements are marked optional with a question mark, which matches how the data actually behaves and keeps the types honest.

It infers, it does not enforce a schema

One important point: it infers a shape from the single sample you give it, rather than enforcing a schema. If a field is sometimes null or absent in real data but not in your sample, the generated type will not know that, so use a representative example.

常见问题

嵌套对象如何处理?

每个嵌套对象会成为各自命名的接口,由其父级引用,因此结果整洁且可复用。

对于形状不同的数组怎么办?

对象数组会合并为一个接口,某些元素缺少的键会被标记为可选。混合基本类型的数组会成为联合元素类型。

我的 JSON 会被上传到任何地方吗?

不会。类型完全在你的浏览器中生成,因此你的数据永远不会离开你的设备。

来源

嵌入此工具

将此工具添加到你自己的网站。复制下方代码片段即可,它会自动保持最新。

<iframe src="https://monu.tools/embed/zh/json-to-typescript" width="100%" height="640" style="border:1px solid #e5e5e5;border-radius:12px;max-width:680px" loading="lazy" title="Monu Tools"></iframe>

相关工具