Skip to content

Commit

Permalink
fix: Unknown type "undefined" when using generics without base type (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette authored May 29, 2024
1 parent e94df02 commit f411396
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/NodeParser/ArrayNodeParser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import ts from "typescript";
import { Context, NodeParser } from "../NodeParser.js";
import { SubNodeParser } from "../SubNodeParser.js";
import type { Context, NodeParser } from "../NodeParser.js";
import type { SubNodeParser } from "../SubNodeParser.js";
import { AnyType } from "../Type/AnyType.js";
import { ArrayType } from "../Type/ArrayType.js";
import { BaseType } from "../Type/BaseType.js";
import type { BaseType } from "../Type/BaseType.js";

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

public createType(node: ts.ArrayTypeNode, context: Context): BaseType {
const type = this.childNodeParser.createType(node.elementType, context);
return new ArrayType(type);
// Generics without `extends` or `defaults` cannot be resolved, so we fallback to `any`
return new ArrayType(type ?? new AnyType());
}
}
1 change: 1 addition & 0 deletions test/valid-data-other.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe("valid-data-other", () => {
it("array-min-items-2", assertValidSchema("array-min-items-2", "MyType"));
it("array-min-max-items", assertValidSchema("array-min-max-items", "MyType"));
it("array-min-max-items-optional", assertValidSchema("array-min-max-items-optional", "MyType"));
it("array-function-generics", assertValidSchema("array-function-generics", "*"));
it("array-max-items-optional", assertValidSchema("array-max-items-optional", "MyType"));
it("shorthand-array", assertValidSchema("shorthand-array", "MyType"));

Expand Down
4 changes: 4 additions & 0 deletions test/valid-data/array-function-generics/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function arrayGenerics<T>(a: T[], b: T[]): T[] {
console.log(a, b);
return b;
}
30 changes: 30 additions & 0 deletions test/valid-data/array-function-generics/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$ref": "#/definitions/arrayGenerics",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"arrayGenerics": {
"$comment": "(a: T[], b: T[]) => T[]",
"type": "object",
"properties": {
"namedArgs": {
"type": "object",
"properties": {
"a": {
"type": "array",
"items": {}
},
"b": {
"type": "array",
"items": {}
}
},
"required": [
"a",
"b"
],
"additionalProperties": false
}
}
}
}
}

0 comments on commit f411396

Please sign in to comment.