bcherny/json-schema-to-typescript

Example for multiple Types needed

coolusaHD opened this issue · 4 comments

Hello,

can you provide an example where I can generate multiple types with only one JSON file.

I browsed a bit to your code but couldn't find anything.

Thanks in advance.

Can you comment with your JSON file, and the output you’d expect?

Thanks for the quick answer @bcherny .

I would like as result something like:

export interface a {
    name: string;
}

export interface b {
    age: number;
}

But I don't know how I can structure the json to get this output.

Can you provide me an example JSON for that ?

Thanks

my currently workaround is

{
  "title": "All",
  "description": "xyz",
  "oneOf": [
    {
      "$ref": "#/definitions/test1"
    },
    {
      "$ref": "#/definitions/test2"
    }
  ],
  "definitions": {
    "test1": {
      "type": "object",
      "description": "aaaa",
      "properties": {
        "a": {
          "type": "string"
        }
      },
      "additionalProperties": false,
      "required": [
        "test5"
      ]
    },
    "test2": {
      "properties": {
        "b": {
          "type": "string"
        }
      },
      "additionalProperties": false
    }
  }
}

which generates to:

/* tslint: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.
 */

/**
 * xyz
 */
export type All = Test1 | Test2;

/**
 * aaaa
 */
export interface Test1 {
  a?: string;
}
export interface Test2 {
  b?: string;
}

But if I remove the oneOf it generates

/* tslint: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.
 */

/**
 * xyz
 */
export interface All {
  [k: string]: unknown;
}

Can you see my error ?

You want to enable the unreachableDefinitions option, then remove the allOf:

{
  "title": "All",
  "description": "xyz",
  "definitions": {
    "test1": {
      "type": "object",
      "description": "aaaa",
      "properties": {
        "a": {
          "type": "string"
        }
      },
      "additionalProperties": false,
      "required": [
        "test5"
      ]
    },
    "test2": {
      "properties": {
        "b": {
          "type": "string"
        }
      },
      "additionalProperties": false
    }
  }
}

Output:

/* 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.
 */

/**
 * xyz
 */
export interface All {
  [k: string]: unknown;
}
/**
 * aaaa
 *
 * This interface was referenced by `All`'s JSON-Schema
 * via the `definition` "test1".
 */
export interface Test1 {
  a?: string;
}
/**
 * This interface was referenced by `All`'s JSON-Schema
 * via the `definition` "test2".
 */
export interface Test2 {
  b?: string;
}