Skip to content

Commit f411396

Browse files
fix: Unknown type "undefined" when using generics without base type (#1978)
1 parent e94df02 commit f411396

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

src/NodeParser/ArrayNodeParser.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import ts from "typescript";
2-
import { Context, NodeParser } from "../NodeParser.js";
3-
import { SubNodeParser } from "../SubNodeParser.js";
2+
import type { Context, NodeParser } from "../NodeParser.js";
3+
import type { SubNodeParser } from "../SubNodeParser.js";
4+
import { AnyType } from "../Type/AnyType.js";
45
import { ArrayType } from "../Type/ArrayType.js";
5-
import { BaseType } from "../Type/BaseType.js";
6+
import type { BaseType } from "../Type/BaseType.js";
67

78
export class ArrayNodeParser implements SubNodeParser {
89
public constructor(protected childNodeParser: NodeParser) {}
@@ -13,6 +14,7 @@ export class ArrayNodeParser implements SubNodeParser {
1314

1415
public createType(node: ts.ArrayTypeNode, context: Context): BaseType {
1516
const type = this.childNodeParser.createType(node.elementType, context);
16-
return new ArrayType(type);
17+
// Generics without `extends` or `defaults` cannot be resolved, so we fallback to `any`
18+
return new ArrayType(type ?? new AnyType());
1719
}
1820
}

test/valid-data-other.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ describe("valid-data-other", () => {
7878
it("array-min-items-2", assertValidSchema("array-min-items-2", "MyType"));
7979
it("array-min-max-items", assertValidSchema("array-min-max-items", "MyType"));
8080
it("array-min-max-items-optional", assertValidSchema("array-min-max-items-optional", "MyType"));
81+
it("array-function-generics", assertValidSchema("array-function-generics", "*"));
8182
it("array-max-items-optional", assertValidSchema("array-max-items-optional", "MyType"));
8283
it("shorthand-array", assertValidSchema("shorthand-array", "MyType"));
8384

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function arrayGenerics<T>(a: T[], b: T[]): T[] {
2+
console.log(a, b);
3+
return b;
4+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$ref": "#/definitions/arrayGenerics",
3+
"$schema": "http://json-schema.org/draft-07/schema#",
4+
"definitions": {
5+
"arrayGenerics": {
6+
"$comment": "(a: T[], b: T[]) => T[]",
7+
"type": "object",
8+
"properties": {
9+
"namedArgs": {
10+
"type": "object",
11+
"properties": {
12+
"a": {
13+
"type": "array",
14+
"items": {}
15+
},
16+
"b": {
17+
"type": "array",
18+
"items": {}
19+
}
20+
},
21+
"required": [
22+
"a",
23+
"b"
24+
],
25+
"additionalProperties": false
26+
}
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)