Skip to content

Commit c6e651d

Browse files
rpetrichdomoritz
authored andcommitted
Support TypeScript 3.0's support for rest elements as the trailing parameter of tuples
1 parent fd68da4 commit c6e651d

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type MyTuple = [string, ...number[]];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "array",
4+
"items": [
5+
{
6+
"type": "string"
7+
}
8+
],
9+
"additionalItems": {
10+
"type": "number"
11+
},
12+
"minItems": 1
13+
}

Diff for: test/schema.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ describe("schema", () => {
161161
assertSchema("type-aliases-recursive-export", "MyObject");
162162
*/
163163
assertSchema("type-aliases-tuple-of-variable-length", "MyTuple");
164+
assertSchema("type-aliases-tuple-with-rest-element", "MyTuple");
164165
});
165166

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

Diff for: typescript-json-schema.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export type Definition = {
8080
minItems?: number,
8181
additionalItems?: {
8282
anyOf: Definition[]
83-
},
83+
} | Definition,
8484
enum?: PrimitiveType[] | Definition[],
8585
default?: PrimitiveType | Object,
8686
additionalProperties?: Definition | boolean,
@@ -380,9 +380,14 @@ export class JsonSchemaGenerator {
380380
definition.items = fixedTypes;
381381
const targetTupleType = (propertyType as ts.TupleTypeReference).target;
382382
definition.minItems = targetTupleType.minLength;
383-
definition.additionalItems = {
384-
anyOf: fixedTypes
385-
};
383+
if (targetTupleType.hasRestElement) {
384+
definition.additionalItems = fixedTypes[fixedTypes.length - 1];
385+
fixedTypes.splice(fixedTypes.length - 1, 1);
386+
} else {
387+
definition.additionalItems = {
388+
anyOf: fixedTypes
389+
};
390+
}
386391
} else {
387392
const propertyTypeString = this.tc.typeToString(propertyType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType);
388393
const flags = propertyType.flags;

0 commit comments

Comments
 (0)