Skip to content
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

fix: typeof function #1182

Merged
merged 2 commits into from Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion factory/formatter.ts
Expand Up @@ -28,6 +28,7 @@ import { UnknownTypeFormatter } from "../src/TypeFormatter/UnknownTypeFormatter"
import { VoidTypeFormatter } from "../src/TypeFormatter/VoidTypeFormatter";
import { MutableTypeFormatter } from "../src/MutableTypeFormatter";
import { NeverTypeFormatter } from "../src/TypeFormatter/NeverTypeFormatter";
import { FunctionTypeFormatter } from "../src/TypeFormatter/FunctionTypeFormatter";

export type FormatterAugmentor = (
formatter: MutableTypeFormatter,
Expand Down Expand Up @@ -74,7 +75,8 @@ export function createFormatter(config: Config, augmentor?: FormatterAugmentor):
.addTypeFormatter(new ArrayTypeFormatter(circularReferenceTypeFormatter))
.addTypeFormatter(new TupleTypeFormatter(circularReferenceTypeFormatter))
.addTypeFormatter(new UnionTypeFormatter(circularReferenceTypeFormatter))
.addTypeFormatter(new IntersectionTypeFormatter(circularReferenceTypeFormatter));
.addTypeFormatter(new IntersectionTypeFormatter(circularReferenceTypeFormatter))
.addTypeFormatter(new FunctionTypeFormatter());

return circularReferenceTypeFormatter;
}
4 changes: 4 additions & 0 deletions src/NodeParser/TypeofNodeParser.ts
Expand Up @@ -38,6 +38,10 @@ 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) {
if ((<ts.FunctionDeclaration>valueDec).type! !== undefined) {
return this.childNodeParser.createType(<ts.Node>(<ts.FunctionDeclaration>valueDec).type, context);
}
}

throw new LogicError(`Invalid type query "${valueDec.getFullText()}" (ts.SyntaxKind = ${valueDec.kind})`);
Expand Down
28 changes: 28 additions & 0 deletions src/TypeFormatter/FunctionTypeFormatter.ts
@@ -0,0 +1,28 @@
import { Definition } from "../Schema/Definition";
import { SubTypeFormatter } from "../SubTypeFormatter";
import { BaseType } from "../Type/BaseType";
import { FunctionType } from "../Type/FunctionType";

export class FunctionTypeFormatter implements SubTypeFormatter {
public supportsType(type: FunctionType): boolean {
return type instanceof FunctionType;
}

public getDefinition(type: FunctionType): Definition {
// Return a custom schema for the function property.
return {
type: "object",
properties: {
isFunction: {
type: "boolean",
const: true,
},
},
};
}

// If this type does NOT HAVE children, generally all you need is:
loopingz marked this conversation as resolved.
Show resolved Hide resolved
public getChildren(type: FunctionType): BaseType[] {
return [];
}
}
1 change: 1 addition & 0 deletions test/valid-data-type.test.ts
Expand Up @@ -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"));
Expand Down
3 changes: 3 additions & 0 deletions test/valid-data/type-typeof-function/main.ts
@@ -0,0 +1,3 @@
import { ConnectionOptions } from "node:tls";

export interface Test extends ConnectionOptions {}
loopingz marked this conversation as resolved.
Show resolved Hide resolved