diff --git a/src/NodeParser/TypeofNodeParser.ts b/src/NodeParser/TypeofNodeParser.ts index 2f77121c0..42e1bd10d 100644 --- a/src/NodeParser/TypeofNodeParser.ts +++ b/src/NodeParser/TypeofNodeParser.ts @@ -7,6 +7,7 @@ import { ObjectType, ObjectProperty } from "../Type/ObjectType"; import { ReferenceType } from "../Type/ReferenceType"; import { getKey } from "../Utils/nodeKey"; import { LiteralType } from "../Type/LiteralType"; +import { UnknownType } from "../Type/UnknownType"; export class TypeofNodeParser implements SubNodeParser { public constructor(protected typeChecker: ts.TypeChecker, protected childNodeParser: NodeParser) {} @@ -38,6 +39,14 @@ export class TypeofNodeParser implements SubNodeParser { return this.childNodeParser.createType(valueDec, context); } else if (ts.isPropertyAssignment(valueDec)) { return this.childNodeParser.createType(valueDec.initializer, context); + } else if (valueDec.kind === ts.SyntaxKind.FunctionDeclaration) { + // Silently ignoring Function as JSON Schema does not define them + // see https://github.com/vega/ts-json-schema-generator/issues/98 + return new UnknownType( + `Function:(${(valueDec).parameters.map((p) => p.getFullText()).join(",")}): ${(< + ts.FunctionDeclaration + >valueDec).type?.getFullText()}` + ); } throw new LogicError(`Invalid type query "${valueDec.getFullText()}" (ts.SyntaxKind = ${valueDec.kind})`); diff --git a/src/Type/UnknownType.ts b/src/Type/UnknownType.ts index 24967695b..999888370 100644 --- a/src/Type/UnknownType.ts +++ b/src/Type/UnknownType.ts @@ -1,7 +1,14 @@ import { BaseType } from "./BaseType"; export class UnknownType extends BaseType { + constructor(private comment?: string) { + super(); + } public getId(): string { return "unknown"; } + + public getComment(): string | undefined { + return this.comment; + } } diff --git a/src/TypeFormatter/UnknownTypeFormatter.ts b/src/TypeFormatter/UnknownTypeFormatter.ts index 444b9f00b..bcf52c508 100644 --- a/src/TypeFormatter/UnknownTypeFormatter.ts +++ b/src/TypeFormatter/UnknownTypeFormatter.ts @@ -8,7 +8,9 @@ export class UnknownTypeFormatter implements SubTypeFormatter { return type instanceof UnknownType; } public getDefinition(type: UnknownType): Definition { - return {}; + return { + $comment: type.getComment(), + }; } public getChildren(type: UnknownType): BaseType[] { return []; diff --git a/test/valid-data-type.test.ts b/test/valid-data-type.test.ts index a05bc9c3d..405351784 100644 --- a/test/valid-data-type.test.ts +++ b/test/valid-data-type.test.ts @@ -62,6 +62,7 @@ describe("valid-data-type", () => { it("type-typeof-class-static-property", assertValidSchema("type-typeof-class-static-property", "MyType")); it("type-typeof-enum", assertValidSchema("type-typeof-enum", "MyObject")); it("type-typeof-class", assertValidSchema("type-typeof-class", "MyObject")); + it("type-typeof-function", assertValidSchema("type-typeof-function", "*")); it("type-keys", assertValidSchema("type-typeof-keys", "MyType")); it("type-indexed-access-tuple-1", assertValidSchema("type-indexed-access-tuple-1", "MyType")); diff --git a/test/valid-data/type-typeof-function/main.ts b/test/valid-data/type-typeof-function/main.ts new file mode 100644 index 000000000..ad56e5c8e --- /dev/null +++ b/test/valid-data/type-typeof-function/main.ts @@ -0,0 +1,7 @@ +function checkServerIdentity(hostname: string): Error | undefined { + return undefined; +} +export interface Test { + checkServerIdentity?: typeof checkServerIdentity | undefined; + otherProp: string; +} diff --git a/test/valid-data/type-typeof-function/schema.json b/test/valid-data/type-typeof-function/schema.json new file mode 100644 index 000000000..d57e1f056 --- /dev/null +++ b/test/valid-data/type-typeof-function/schema.json @@ -0,0 +1,32 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "NamedParameters": { + "additionalProperties": false, + "properties": { + "hostname": { + "type": "string" + } + }, + "required": [ + "hostname" + ], + "type": "object" + }, + "Test": { + "additionalProperties": false, + "properties": { + "checkServerIdentity": { + "$comment": "Function:(hostname: string): Error | undefined" + }, + "otherProp": { + "type": "string" + } + }, + "required": [ + "otherProp" + ], + "type": "object" + } + } +}