Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added function type definition
  • Loading branch information
Tiberiu02 committed Oct 10, 2022
commit e90789e73aa123d80db61496fdb5e23670019118
31 changes: 23 additions & 8 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,16 +553,16 @@ export class JsonSchemaGenerator {
if (comments.length) {
definition.description = comments
.map((comment) => {
const newlineNormalizedComment = comment.text.replace(/\r\n/g, "\n");
const newlineNormalizedComment = comment.text.replace(/\r\n/g, "\n");

// If a comment contains a "{@link XYZ}" inline tag that could not be
// resolved by the TS checker, then this comment will contain a trailing
// whitespace that we need to remove.
if (comment.kind === "linkText") {
return newlineNormalizedComment.trim();
}
// If a comment contains a "{@link XYZ}" inline tag that could not be
// resolved by the TS checker, then this comment will contain a trailing
// whitespace that we need to remove.
if (comment.kind === "linkText") {
return newlineNormalizedComment.trim();
}

return newlineNormalizedComment;
return newlineNormalizedComment;
})
.join("").trim();
}
Expand Down Expand Up @@ -1003,6 +1003,19 @@ export class JsonSchemaGenerator {
return definition;
}

private getFunctionDefinition(funcType: ts.Type, definition: Definition): Definition {
const node = funcType.getSymbol()!.getDeclarations()![0];

// tiberiu
const parameters = (node as ts.FunctionLikeDeclaration).parameters;
definition.typeof = "function";
definition.parameters = parameters
.map((p) => this.tc.getTypeAtLocation(p.type))
.map((type) => this.getTypeDefinition(type));

return definition;
}

private getClassDefinition(clazzType: ts.Type, definition: Definition): Definition {
const node = clazzType.getSymbol()!.getDeclarations()![0];

Expand Down Expand Up @@ -1375,6 +1388,8 @@ export class JsonSchemaGenerator {
// {} is TypeLiteral with no members. Need special case because it doesn't have declarations.
definition.type = "object";
definition.properties = {};
} else if (ts.isFunctionLike(node)) {
this.getFunctionDefinition(typ, definition);
} else {
this.getClassDefinition(typ, definition);
}
Expand Down