如何让json-schema-to-typescript正确生成关联的Dog类型?
问题
使用json-schema-to-typescript工具的CLI命令json2ts -i schema/ -o types/从JSON Schema生成TypeScript类型。schema目录包含elephant.json和types.json两个文件:
types.json中定义了Dog类型elephant.json声明了Dog数组字段dogs,期望生成dogs: Dog[]的代码
添加unreachableDefinitions参数后,Dog类型已成功生成,但Elephant的dogs字段仍被生成为{[k: string]: unknown}[],未正确引用Dog类型。需要实现生成带import { Dog } from "./types"且dogs字段为Dog[]的代码。
相关文件内容
types.json
{"$schema": "http://json-schema.org/draft-07/schema#","definitions": {"Dog": {"type": "object","properties": {"name": {"type": "string","minLength": 1},"age": {"type": "integer","minimum": 0},"breed": {"type": "string"},"color": {"type": "string","enum": ["black","brown","white","golden"]}},"additionalProperties": false,"required": ["name","age"]},"Cat": {"type": "object"}}}
elephant.json
{"$schema": "http://json-schema.org/draft-07/schema#","title": "Elephant","type": "object","properties": {"name": {"type": "string","minimum": 3,"maximum": 12},"age": {"type": "integer","minimum": 0},"gender": {"type": "string","enum": ["male", "female"]},"weight": {"description": "must be heavy..","type": "number","minimum": 0,"default": 30},"height": {"type": "number","minimum": 0,"default": 100},"dogs": {"description": "Yes... every elephant needs a dog or two :-) ","type": "array","items": {"$ref": "schema/types.json#/definitions/Dog"},"minimum": 1,"maximum": 2},"color": {"type": "string"}},"additionalProperties": false,"required": ["name", "age", "gender","height","dogs"]}
当前生成的elephant.d.ts
/* eslint-disable */ /** * This file was automatically generated by json-schema-to-typescript. * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, * and run json-schema-to-typescript to regenerate this file. */ export interface Elephant { name: string; age: number; gender: "male" | "female"; /** * must be heavy.. */ weight?: number; height: number; /** * Yes... every elephant needs a dog or two :-) */ dogs: { [k: string]: unknown; }[]; color?: string; }
期望生成的代码
/* eslint-disable */ /** * This file was automatically generated by json-schema-to-typescript. * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, * and run json-schema-to-typescript to regenerate this file. */ import { Dog } from "./types"; export interface Elephant { name: string; age: number; gender: "male" | "female"; /** * must be heavy.. */ weight?: number; height: number; /** * Yes... every elephant needs a dog or two :-) */ dogs: Dog[]; color?: string; }
解决方案
1. 修正JSON Schema中的$ref路径
elephant.json中dogs字段的$ref使用了错误的路径,需要改为相对于当前文件的路径:
将
"$ref": "schema/types.json#/definitions/Dog"
修改为
"$ref": "./types.json#/definitions/Dog"
工具从schema/目录读取文件时,相对路径需基于该目录下的文件位置。
2. 调整CLI命令参数
执行以下命令确保启用外部引用处理:
json2ts -i schema/ -o types/ --unreachableDefinitions --refs external
--unreachableDefinitions:确保生成types.json中未被直接引用的Dog类型--refs external:强制将跨文件引用生成为import语句,而非内联类型
验证结果
修改后重新执行命令,生成的elephant.d.ts会自动添加import { Dog } from "./types",且dogs字段会正确生成为Dog[],与期望代码一致。
内容的提问来源于stack exchange,提问作者balderman
相关产品推荐
相关产品推荐

