Skip to main content
DevTools24

JSON 转 TypeScript

从 JSON 数据生成 TypeScript 接口或类型别名。嵌套对象会自动转换为单独的类型。

About Type Generation

  • Nested objects are converted to separate interfaces/types
  • Arrays are typed based on their first element
  • Use "interface" for extendable types, "type" for unions/intersections
  • Optional (?) properties work well when some fields may be undefined

TypeScript Types - 技术详情

TypeScript interfaces define the shape of objects for compile-time type checking. Generating types from JSON is useful when working with API responses, configuration files, or any structured data.

命令行替代方案

# Generate types with quicktype CLI\nnpx quicktype --lang ts --src data.json\n\n# Or use json-to-ts npm package\nnpx json-to-ts data.json

参考

查看官方规范