I am using json-schema-to-typescript in order to generate typescript types from json schema.
I am using the cli command:
json2ts -i schema/ -o types/
inside the schema folder I have 2 files:
In types.json there is a definition of a Dog.
In elephant.json I declare array of Dog
I am expecting that the generated code code will have the field:
dogs: Dog[]
However the type Dog is not generated.
How can I make the code generator generate the Dog type and declare the array of dogs properly?
Below are my schema files and the generated typescript code:
{
"$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"}
}
}
{
"$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": {
"type": { "$ref": "schema/types.json#/definitions/Dog" }
},
"minimum": 1,
"maximum": 2
},
"color": {
"type": "string"
}
},
"additionalProperties": false,
"required": ["name", "age", "gender","height","dogs"]
}
/* 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.
*/
export interface Types {
[k: string]: unknown;
}
Thanks to @Dimava I have added the cli flag unreachableDefinitions and now the "types" (Dog,Cat) are generated.
However the Elephant is still not using the Dog when it declare the dogs array. It still looks like:
dogs: {
[k: string]: unknown;
}[];
/**
* This interface was referenced by `Types`'s JSON-Schema
* via the `definition` "Dog".
*/
export interface Dog {
name: string;
age: number;
breed?: string;
color?: "black" | "brown" | "white" | "golden";
}
/**
* This interface was referenced by `Types`'s JSON-Schema
* via the `definition` "Cat".
*/
export interface Cat {
breed?: string;
}
Can this code generator generate such code?
* 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;
}
You want to enable the unreachableDefinitions option
| unreachableDefinitions | boolean | false | Generates code for $defs that aren't referenced by the schema |
|---|
https://github.com/bcherny/json-schema-to-typescript/issues/494
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With