使用 openapi-typescript-codegen 自动生成 openapi 接口文档的 TypeScript 类型
2025年5月19日 17:20
安装依赖
npm i openapi-typescript-codegen -D
配置生成脚本
这里配置了只生成接口数据类型的定义,需要生成每个 API 的请求封装需要修改 参数
"generate-api": "npx openapi-typescript-codegen --input http://xxx/v2/api-docs --output ./src/api/generated --exportCore false --exportServices false",
脚本中的 http://xxx/v2/api-docs
换成你接口文档的 JSON 数据源地址,可以在浏览器控制台找到
生成结果
执行 npm run generate-api
后就能看到一堆类型定义
使用
然后就可以愉快的使用自动生成的类型定义了