ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

Broken output in presence of type manipulations

Open CComparon opened this issue 6 months ago • 1 comments

Hi,

I'd like to know if this is a known issue and whether there's a chance that ts-json-schema-generator addresses such cases. This is currently preventing me from migrating away from typescript-json-schema which has its own limitations.

NB: the only change between the Good and the Bad case below is the position of RemoveNulls, which should be functionnally equivalent.

Good 👍

Input:

type RemoveNulls<T extends object> = { [P in keyof T]: Exclude<T[P], null> };

type IdType = number;

type XBaseType = {
  str: IdType | null;
};

type XNullableType = RemoveNulls<XBaseType> & {
  bar?: string;
};

export type XType = XNullableType;

Output:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "bar": {
      "type": "string"
    },
    "str": {
      "type": "number"
    }
  },
  "required": [
    "str"
  ],
  "definitions": {}
}

Bad 👎🏻

Input:

type RemoveNulls<T extends object> = { [P in keyof T]: Exclude<T[P], null> };

type IdType = number;

type XBaseType = {
  str: IdType | null;
};

type XNullableType = XBaseType & {
  bar?: string;
};

export type XType = RemoveNulls<XNullableType>;

Output: (str is mangled!)

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "str": {
      "not": {}
    },
    "bar": {
      "type": "string"
    }
  },
  "required": [
    "str"
  ],
  "definitions": {}
}

Keep up the great work, Cyril

CComparon avatar Apr 22 '25 22:04 CComparon