Open cdaringe opened 4 years ago
Having the same issue.
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 :)
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
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.
is https://github.com/vega/ts-json-schema-generator a good alternative?
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.
it seems not, is more broken than this library. fails for more scenarios.
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.
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
problem
consider
Record<string, true>
, and that it outputs:$ref: #/definitions/Record<string, true>
, where#/definitions/Record<string, true>
in the definitions object is: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!