停止手动编写 TypeScript 接口 — 自动转换 JSON
发布: (2026年3月31日 GMT+8 22:47)
2 分钟阅读
原文: Dev.to
Source: Dev.to
你有多少次收到 API 的 JSON 响应后,需要手动编写 TypeScript 接口?
我做了一个免费工具可以瞬间完成这件事:JSON to TypeScript Converter。
粘贴这段 JSON
{
"id": 1,
"name": "Alice",
"email": "alice@example.com",
"address": {
"street": "123 Main St",
"city": "Springfield",
"zip": "62701"
},
"orders": [
{ "id": 101, "total": 29.99, "status": "shipped" }
]
}获得以下 TypeScript
interface Address {
street: string;
city: string;
zip: string;
}
interface OrdersItem {
id: number;
total: number;
status: string;
}
interface Root {
id: number;
name: string;
email: string;
address: Address;
orders: OrdersItem[];
}转换器的功能
- 嵌套对象 – 自动创建独立的接口
- 数组 – 推断元素类型
- 混合类型 – 如
string | number - 空值 – 视为
unknown
我为何开发它
我在开发 SnapAPI —— 一个可以从 JSON 快速生成 REST API 的工具时,常常需要把 API 响应转换成 TypeScript。于是这个转换器就作为独立的实用工具诞生了。
其他免费工具
- JSON Formatter – 美化和压缩 JSON
- JSON Validator – 实时语法校验
- Fake Data Generator – 生成逼真的测试数据
所有工具均免费,无需注册,且开源。