Skip to content

Commit

Permalink
Fixed #1532 satisfies support
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Aug 14, 2023
1 parent e3abf88 commit 772429c
Show file tree
Hide file tree
Showing 5 changed files with 34 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 @@ -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"
}
}
}

0 comments on commit 772429c

Please sign in to comment.