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

feat: satisfies support #1748

Merged
merged 1 commit into from
Aug 14, 2023
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
2 changes: 2 additions & 0 deletions factory/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { UnknownTypeNodeParser } from "../src/NodeParser/UnknownTypeNodeParser";
import { VoidTypeNodeParser } from "../src/NodeParser/VoidTypeNodeParser";
import { SubNodeParser } from "../src/SubNodeParser";
import { TopRefNodeParser } from "../src/TopRefNodeParser";
import { SatisfiesNodeParser } from "../src/NodeParser/SatisfiesNodeParser";

export type ParserAugmentor = (parser: MutableParser) => void;

Expand Down Expand Up @@ -104,6 +105,7 @@ export function createParser(program: ts.Program, config: Config, augmentor?: Pa
.addNodeParser(new NeverTypeNodeParser())
.addNodeParser(new ObjectTypeNodeParser())
.addNodeParser(new AsExpressionNodeParser(chainNodeParser))
.addNodeParser(new SatisfiesNodeParser(chainNodeParser))
.addNodeParser(new FunctionParser(chainNodeParser))
.addNodeParser(withJsDoc(new ParameterParser(chainNodeParser)))
.addNodeParser(new StringLiteralNodeParser())
Expand Down
15 changes: 15 additions & 0 deletions src/NodeParser/SatisfiesNodeParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import ts from "typescript";
import { Context, NodeParser } from "../NodeParser";
import { SubNodeParser } from "../SubNodeParser";
import { BaseType } from "../Type/BaseType";

export class SatisfiesNodeParser implements SubNodeParser {
public constructor(protected childNodeParser: NodeParser) {}

public supportsNode(node: ts.SatisfiesExpression): boolean {
return node.kind === ts.SyntaxKind.SatisfiesExpression;
}
public createType(node: ts.SatisfiesExpression, context: Context): BaseType {
return this.childNodeParser.createType(node.expression, context);
}
}
2 changes: 2 additions & 0 deletions test/valid-data-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,7 @@ describe("valid-data-type", () => {
it("type-tuple-nested-rest-uniform", assertValidSchema("type-tuple-nested-rest-uniform", "MyType"));

it("type-recursive-deep-exclude", assertValidSchema("type-recursive-deep-exclude", "MyType"));
it("type-satisfies", assertValidSchema("type-satisfies", "MyType"));

it("ignore-export", assertValidSchema("ignore-export", "*"));
});
2 changes: 2 additions & 0 deletions test/valid-data/type-satisfies/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const record = { x: "hello", y: "goodbye" } satisfies Record<string, string>;
export type MyType = keyof typeof record;
13 changes: 13 additions & 0 deletions test/valid-data/type-satisfies/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$ref": "#/definitions/MyType",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyType": {
"enum": [
"x",
"y"
],
"type": "string"
}
}
}