typescript-json-schema
typescript-json-schema copied to clipboard
Optional array / tuple elements
I have defined a tuple type with an optional element like this:
type tuple23 = [ number, string, string | undefined ];
The translation I get is:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"additionalItems": {
"anyOf": [
{
"type": "number"
},
{
"type": "string"
},
{
"type": "string"
}
]
},
"items": [
{
"type": "number"
},
{
"type": "string"
},
{
"type": "string"
}
],
"minItems": 3,
"type": "array"
}
The problem is that the undefined
has been lost: minItems
should be 2. What I expect is:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"items": [
{
"type": "number"
},
{
"type": "string"
},
{
"type": "string"
}
],
"minItems": 2,
"maxItems": 3,
"type": "array"
}
Same thing happens for me: When using type Array<SomeInterface | null>, typescript-json-schema ignores the null type in the "items" property.
same issue for me
+1 To this. I would like Array<string | null> to turn into "items": {"type": ["string", "null"] }
Happy to review PRs for this bug.
Same issue here. Undefined is missing.
something?: number | undefined;
in an array becomes
{"type":"object","properties":{"something":{"type":"number"}
and undefined just goes puff!
@domoritz @ali-habibzadeh The Issue is still present now. The issue is of general nature:
For a property myProp: number | undefined;
(in an interface/class/type alias) the undefined
will be swollowed which is not to be expected.
In the https://github.com/vega/ts-json-schema-generator library which is inspired from this lib, this issue of disappearing undefined
types doesn't seem to occur (see https://github.com/vega/ts-json-schema-generator/issues/623 - is this true @ali-habibzadeh ? You opened that issue - I couldn't confirm it because of another issue in that lib I'm facing (i.e. https://github.com/vega/ts-json-schema-generator/issues/1456 ))
When using the --required
flag, myProp
will not be in the required
field, although it does not have a question mark (i.e. optional modifier). Using ?
or | undefined
are different in TypeScript and this library does not take that into account.
@andrekovac I think it would be useful to contribute your stripped use case as a unit test so maintainers or contributors of this library, can easily patch it, or if you want to also contribute the patch.
If you follow that method a simple unit test with:
interface UnionTypeWithNull {
myParam: string | null;
}
This test should not be blocked by the issue: https://github.com/vega/ts-json-schema-generator/issues/1456
Also the case for undefined
does not seem relevant to me, myParam: string | undefined
is in fact equal to myParam?: string
which is managed by this lib to my knowledge by not adding the myParam
to the required
fields
The other case from the issue is about within an array which is different as the array item definition is handled differently.
@loopingz There's a crucial difference with undefined
. Facing an issue concerning this difference in my codebase lead me to find the issue here. In fact the case myParam: string | undefined
is NOT equal to myParam?: string
. I created this TS playground as an example.
So this difference is not covered by this library. I created a separate issue for it because it is different to this one: https://github.com/YousefED/typescript-json-schema/issues/551
There I also mentioned that it might be difficult to fix because undefined
is not a valid value in JSON.