YousefED / typescript-json-schema

Generate json-schema from your Typescript sources
BSD 3-Clause "New" or "Revised" License
3.17k stars 323 forks source link

first class TS advances types stringified instead of expanded #337

Open cdaringe opened 4 years ago

cdaringe commented 4 years ago

problem

consider Record<string, true>, and that it outputs: $ref: #/definitions/Record<string, true>, where #/definitions/Record<string, true> in the definitions object is:

    "Record<string,Record<string,true>>": {
      "description": "Construct a type with a set of properties K of type T",
      "type": "object"
    },

discussion

i would expect that such an abstract type may be unpacked into more specific things or compiled into an intermediate representation that is { [key: string]: true } which would yield nicer definitions.

i'm a rook w/ some of this stuff, so i may have not used the correct verbiage. thx!

rix0rrr commented 4 years ago

Having the same issue.

maciekgrzybek commented 3 years ago

I'm getting similar problem, I have these types:

type MarketType = Record<string, { header: HeaderView }>;

export interface CompetitionEventsView {
  marketTypes: MarketType[];
  events: EventView[];
}

And in the schema I'm getting back this:

  "properties": {
    "marketTypes": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/Record<string,{header:HeaderView;}>"
      }
    },
    "events": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/EventView"
      }
    }
  }

Any idea if that could be fixed? If time is an issue, I'm happy to take a look by myself, I would just need some pointing in the right direction on where I could start :)

dimaMachina commented 2 years ago

any updates on it? The solution it's seems very simple, typescript-json-schema should behave with Record type as it interface type with index signature

ferm10n commented 2 years ago

Here's something I just discovered that might give some clues.

Record<'one', 'two', number> correctly produces:

"Record<\"one\"|\"two\",number>": {
  "additionalProperties": false,
  "properties": {
    "one": {
      "type": "number"
    },
    "two": {
      "type": "number"
    }
  },
  "required": ["one", "two"],
  "type": "object"
},

So it doesn't seem like Record is just unsupported, but that the way the keys get expanded is where the issue is.

elios264 commented 11 months ago

is https://github.com/vega/ts-json-schema-generator a good alternative?

domoritz commented 11 months ago

I'd say so. It's the generator I maintain (after moving from this one) and we use it for Vega-Lite which is a pretty complex type.

elios264 commented 10 months ago

it seems not, is more broken than this library. fails for more scenarios.

domoritz commented 10 months ago

That's a broad statement that I do not support. The test cases are a superset of the tests here: https://github.com/vega/ts-json-schema-generator/tree/next/test/valid-data. Failures are dependent on your use case.

stuart-clark-45 commented 8 months ago

Work around...

If you start with something like this

interface Dog {
  name: string;
  age: number;
}

interface Example {
   id2Dog: Record<string, Dog>;
}

Change it to

interface Dog {
  name: string;
  age: number;
}

interface Id2Dog {
  [key: string]: Dog;
}

interface Example {
   id2Dog: Id2Dog;
}

And you should get the correct json output