typescript-json-schema icon indicating copy to clipboard operation
typescript-json-schema copied to clipboard

first class TS advances types stringified instead of expanded

Open cdaringe opened this issue 5 years ago • 9 comments

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!

cdaringe avatar Jan 04 '20 01:01 cdaringe

Having the same issue.

rix0rrr avatar Jan 20 '20 14:01 rix0rrr

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 :)

maciekgrzybek avatar Feb 19 '21 16:02 maciekgrzybek

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

dimaMachina avatar Mar 31 '22 20:03 dimaMachina

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.

ferm10n avatar Jul 20 '22 19:07 ferm10n

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

elios264 avatar Dec 20 '23 00:12 elios264

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.

domoritz avatar Dec 20 '23 08:12 domoritz

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

elios264 avatar Jan 23 '24 20:01 elios264

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.

domoritz avatar Jan 23 '24 21:01 domoritz

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

stuart-clark-45 avatar Mar 20 '24 22:03 stuart-clark-45