JSON 到 TypeScript

使用嵌套对象支持从 JSON 生成 TypeScript 接口。

免费在线 JSON 到 TypeScript 生成器

停止 API 响应的手写界面。粘贴 JSON 并获取 TypeScript 接口,其中嵌套对象分为单独的类型和自动推断的数组元素形状。

命名您的根接口,将输出复制到您的项目中,并将其与 fetch 或 HTTP 客户端一起使用以实现完全类型安全。一切都在浏览器的客户端运行。

Frequently Asked Questions

What TypeScript types are generated?
Primitive JSON values map to string, number, boolean, and null. Arrays become Type[] with the element type inferred from the first item. Objects become interfaces with named properties. Nested objects generate separate interface definitions.
How are optional fields handled?
Properties present in the sample JSON are included as required fields. If you need optional fields, add them to your sample JSON with null values, or manually add ? to the generated interface properties.
Can I name the root interface?
Yes. Set a custom root interface name before generating. Nested interfaces are named automatically based on property names (e.g., Address for an address object).
Does this handle union types?
The generator infers types from a single sample. If a field can be string or number, provide samples that include both variants, or manually adjust the generated type to a union (string | number).
Is the output compatible with strict TypeScript?
Yes. Generated interfaces use standard TypeScript syntax compatible with strict mode. Review nullable fields - JSON null maps to null in the type, not undefined. Adjust to field?: Type | null if needed.