Skip to content

Commit

Permalink
feat: support constructor node types (#1512)
Browse files Browse the repository at this point in the history
  • Loading branch information
flugg authored Dec 21, 2022
1 parent df2de31 commit 888f691
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions factory/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { BooleanLiteralNodeParser } from "../src/NodeParser/BooleanLiteralNodePa
import { BooleanTypeNodeParser } from "../src/NodeParser/BooleanTypeNodeParser";
import { CallExpressionParser } from "../src/NodeParser/CallExpressionParser";
import { ConditionalTypeNodeParser } from "../src/NodeParser/ConditionalTypeNodeParser";
import { ConstructorNodeParser } from "../src/NodeParser/ConstructorNodeParser";
import { EnumNodeParser } from "../src/NodeParser/EnumNodeParser";
import { ExpressionWithTypeArgumentsNodeParser } from "../src/NodeParser/ExpressionWithTypeArgumentsNodeParser";
import { FunctionNodeParser } from "../src/NodeParser/FunctionNodeParser";
Expand Down Expand Up @@ -112,6 +113,7 @@ export function createParser(program: ts.Program, config: Config, augmentor?: Pa
.addNodeParser(new BooleanLiteralNodeParser())
.addNodeParser(new NullLiteralNodeParser())
.addNodeParser(new FunctionNodeParser())
.addNodeParser(new ConstructorNodeParser())
.addNodeParser(new ObjectLiteralExpressionNodeParser(chainNodeParser))
.addNodeParser(new ArrayLiteralExpressionNodeParser(chainNodeParser))

Expand Down
18 changes: 18 additions & 0 deletions src/NodeParser/ConstructorNodeParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import ts from "typescript";
import { SubNodeParser } from "../SubNodeParser";
import { BaseType } from "../Type/BaseType";
import { ConstructorType } from "../Type/ConstructorType";

/**
* A constructor node parser that creates a constructor type so that mapped
* types can use constructors as values. There is no formatter for constructor
* types.
*/
export class ConstructorNodeParser implements SubNodeParser {
public supportsNode(node: ts.ConstructorTypeNode): boolean {
return node.kind === ts.SyntaxKind.ConstructorType;
}
public createType(): BaseType {
return new ConstructorType();
}
}
7 changes: 7 additions & 0 deletions src/Type/ConstructorType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BaseType } from "./BaseType";

export class ConstructorType extends BaseType {
public getId(): string {
return "constructor";
}
}
3 changes: 3 additions & 0 deletions test/valid-data-type.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ConstructorType } from "../src/Type/ConstructorType";
import { FunctionType } from "../src/Type/FunctionType";
import { assertMissingFormatterFor, assertValidSchema } from "./utils";

Expand Down Expand Up @@ -127,6 +128,8 @@ describe("valid-data-type", () => {
it("type-conditional-infer-tail-recursion", assertValidSchema("type-conditional-infer-tail-recursion", "MyType"));
it("type-conditional-infer-tuple-xor", assertValidSchema("type-conditional-infer-tuple-xor", "MyType"));

it("type-constructor", assertMissingFormatterFor(new ConstructorType(), "type-constructor", "MyType"));

it("type-tuple-nested-rest", assertValidSchema("type-tuple-nested-rest", "MyType"));
it("type-tuple-nested-rest-to-union", assertValidSchema("type-tuple-nested-rest-to-union", "MyType"));
it("type-tuple-nested-rest-uniform", assertValidSchema("type-tuple-nested-rest-uniform", "MyType"));
Expand Down
5 changes: 5 additions & 0 deletions test/valid-data/type-constructor/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type MyConstructor = new () => any;

export type MyType = {
a: MyConstructor;
};

0 comments on commit 888f691

Please sign in to comment.