Skip to content

Commit bf4b139

Browse files
authored
fix: empty array (YousefED#481)
1 parent 5267246 commit bf4b139

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

test/programs/array-empty/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type MyEmptyArray = [];

test/programs/array-empty/schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "array",
4+
"minItems": 0,
5+
"maxItems": 0
6+
}
7+

test/schema.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ describe("schema", () => {
346346
describe("maps and arrays", () => {
347347
assertSchema("array-readonly", "MyReadOnlyArray");
348348
assertSchema("array-types", "MyArray");
349+
assertSchema("array-empty", "MyEmptyArray");
349350
assertSchema("map-types", "MyObject");
350351
assertSchema("extra-properties", "MyObject");
351352
});

typescript-json-schema.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,9 @@ export class JsonSchemaGenerator {
633633
const elemTypes: ts.NodeArray<ts.TypeNode> = (propertyType as any).typeArguments;
634634
const fixedTypes = elemTypes.map((elType) => this.getTypeDefinition(elType as any));
635635
definition.type = "array";
636-
definition.items = fixedTypes;
636+
if (fixedTypes.length > 0) {
637+
definition.items = fixedTypes;
638+
}
637639
const targetTupleType = (propertyType as ts.TupleTypeReference).target;
638640
definition.minItems = targetTupleType.minLength;
639641
if (targetTupleType.hasRestElement) {

0 commit comments

Comments
 (0)