Skip to content

fix: Unknown type "undefined" when using generics without base type #1978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}
}
}