Skip to content

Commit fd68da4

Browse files
rpetrichdomoritz
authored andcommitted
Support TypeScript 3.0's variable length tuples
1 parent e8a60d2 commit fd68da4

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type MyTuple = [string, number, boolean?];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "array",
4+
"items": [
5+
{
6+
"type": "string"
7+
},
8+
{
9+
"type": "number"
10+
},
11+
{
12+
"type": "boolean"
13+
}
14+
],
15+
"additionalItems": {
16+
"anyOf": [
17+
{
18+
"type": "string"
19+
},
20+
{
21+
"type": "number"
22+
},
23+
{
24+
"type": "boolean"
25+
}
26+
]
27+
},
28+
"minItems": 2
29+
}

Diff for: test/schema.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ describe("schema", () => {
160160
assertSchema("type-aliases-recursive-anonymous", "MyAlias");
161161
assertSchema("type-aliases-recursive-export", "MyObject");
162162
*/
163+
assertSchema("type-aliases-tuple-of-variable-length", "MyTuple");
163164
});
164165

165166
describe("enums", () => {

Diff for: typescript-json-schema.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ export class JsonSchemaGenerator {
378378
const fixedTypes = elemTypes.map(elType => this.getTypeDefinition(elType as any));
379379
definition.type = "array";
380380
definition.items = fixedTypes;
381-
definition.minItems = fixedTypes.length;
381+
const targetTupleType = (propertyType as ts.TupleTypeReference).target;
382+
definition.minItems = targetTupleType.minLength;
382383
definition.additionalItems = {
383384
anyOf: fixedTypes
384385
};

0 commit comments

Comments
 (0)