bcherny/json-schema-to-typescript

properties + anyOf generates incorrect output

Open

#567 创建于 2024年1月11日

在 GitHub 查看
 (4 评论) (12 反应) (0 负责人)TypeScript (449 fork)github user discovery
bughelp wanted

仓库指标

Star
 (3,302 star)
PR 合并指标
 (30 天内没有已合并 PR)

描述

Given this schema:

{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "id": {
      "type": "string"
    },
    "validFrom": {
      "type": "string"
    },
    "validTo": {
      "type": "string"
    }
  },
  "anyOf": [
    {
      "required": ["id", "validFrom"]
    },
    {
      "required": ["id", "validTo"]
    }
  ],
  "title": "Assortment"
}

This is the type generated:

export type Assortment = Assortment1 & Assortment2;
export type Assortment1 = {
  [k: string]: unknown;
};

export interface Assortment2 {
  id?: string;
  validFrom?: string;
  validTo?: string;
}

And I was hoping it would generate something like this:

export type Assortment =
  | {
      id: string;
      validFrom: string;
      validTo?: string;
    }
  | {
      id: string;
      validFrom?: string;
      validTo: string;
    };

I might be doing something wrong, so my apologies in advance.

Thank you so much!

贡献者指南