bcherny/json-schema-to-typescript

Prevent primitive types from being type aliased (when using allOf)

Kruptein opened this issue · 1 comments

I tried moving some things to an allOf setup, which works kinda 1,
but the primitive types are suddenly getting aliased.

e.g.

export type ShapeLocationMoveTarget = PositionTuple & {
  location: Location;
  floor: Floor;
};
export type Location = number;
export type Floor = string;

export interface PositionTuple {
  x: number;
  y: number;
}

For a small setup like this, it's not the worst thing in the world,
but for my real types it's generating hundreds of "useless" types that just bloat the output file.

The above ts output is part of this schema:

{
  "title": "_Master_",
  "type": "object",
  "properties": {
    "PositionTuple": {
      "$ref": "#/definitions/PositionTuple"
    },
    "ShapeLocationMoveTarget": {
      "$ref": "#/definitions/ShapeLocationMoveTarget"
    }
  },
  "required": [
    "PositionTuple",
    "ShapeLocationMoveTarget"
  ],
  "additionalProperties": false,
  "definitions": {
    "PositionTuple": {
      "title": "PositionTuple",
      "type": "object",
      "properties": {
        "x": {
          "type": "integer"
        },
        "y": {
          "type": "integer"
        }
      },
      "required": [
        "x",
        "y"
      ],
      "additionalProperties": false
    },
    "ShapeLocationMoveTarget": {
      "title": "ShapeLocationMoveTarget",
      "allOf": [
        {
          "$ref": "#/definitions/PositionTuple"
        },
        {
          "type": "object",
          "properties": {
            "location": {
              "title": "Location",
              "type": "integer"
            },
            "floor": {
              "title": "Floor",
              "type": "string"
            }
          },
          "required": [
            "location",
            "floor"
          ],
          "additionalProperties": false
        }
      ]
    }
  }
}

Footnotes

  1. It's using type and & instead of interface and extends but I can live with that

This is the expected behavior when you specify an explicit name for your type using title, id, or $id. In this case, you're using title. To avoid the aliases, remove the title from location, floor, etc.