终于不用手写TypeScript中的interface了

209 阅读1分钟

开发痛点

在开发过程中总是要写很多interface很麻烦,严重影响开发效率,就在想有没有能直接生成interface的方法

quicktype

开发过程中一般都会给接口数据增加TS类型,我给大家介绍一个根据接口自动生成TS类型文件的神器:quicktypegithub

quicktype是干嘛的呢:

quicktype 从 JSON、JSON Schema、TypeScript 和 GraphQL 查询生成强类型模型和序列化程序,这使得在许多 编程语言中以类型安全的方式使用 JSON 变得轻而易举。

我们来看下具体功能:

输入代码:
{
  "code": 200,
  "data": {
    "page": 1,
    "page_size": 100,
    "total_count": 3,
    "list": [
      {
        "ts": "2022-12-11",
        "user_id": "31213",
        "origin":55.5,
        "delta": -8,
        "id": "202301060102444",
        "type": "sql",
        "detail": "",
        "ctx": {
          "os": "pc",
          "form": "h5",
          "price": "0.0001",
          "status": "true",
          "test": "4"
        }
      }
    ]
  }
}

输出直接贴图

image.png

VSCode应用

1.应用商店下载Paste JSON as Code

image.png

2.把json对象复制到粘贴板

3.CTRL + shift + p打开命令栏并且选择Paste JSON as Code

image.png

4.输入interface名称

image.png

5.转换成功

image.png