/* 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;
}
generated code (types)
/* 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;
}
After changing the CLI to
json2ts -i schema/ -o types/ --unreachableDefinitions.
The types (Dog,Cat) are generated (see below) but the dogs array is not using the Dog definition as I am expecting.
I am expecting that the generated Elephant will import the Dog and use it in the array declaration.
types.d.ts (after adding the unreachableDefinitions cli flag)
/* 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;
}
/**
* 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;
}
The below code is my expected output
How can I get there ?
/* 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;
}
"minimum" and "maximum" keywords are used for "number" types, "array" types use "minItems" and "maxItems" instead. Also you have to declare the types in "$defs" instead.
I have 2 json schema.
Elephant declare array of dogs and I am expecting the generated code to reflect that. However I don't get array of dogs in the generated TS code.
How can I get array of
Dog
in the generated code?types.json
elephant.json
generated code (elephant.d.ts)
generated code (types)
cli
json2ts --unreachableDefinitions -i schema/ -o types/
Update
After changing the CLI to
json2ts -i schema/ -o types/ --unreachableDefinitions
. The types (Dog,Cat) are generated (see below) but the dogs array is not using the Dog definition as I am expecting.I am expecting that the generated
Elephant
willimport
theDog
and use it in the array declaration.types.d.ts (after adding the unreachableDefinitions cli flag)
The below code is my expected output
How can I get there ?