From 772429cfac624c6ee6b0f629510c011ea251ff97 Mon Sep 17 00:00:00 2001 From: "Amy J. Ko" Date: Mon, 14 Aug 2023 08:31:16 -0700 Subject: [PATCH] Fixed #1532 satisfies support --- factory/parser.ts | 2 ++ src/NodeParser/SatisfiesNodeParser.ts | 15 +++++++++++++++ test/valid-data-type.test.ts | 2 ++ test/valid-data/type-satisfies/main.ts | 2 ++ test/valid-data/type-satisfies/schema.json | 13 +++++++++++++ 5 files changed, 34 insertions(+) create mode 100644 src/NodeParser/SatisfiesNodeParser.ts create mode 100644 test/valid-data/type-satisfies/main.ts create mode 100644 test/valid-data/type-satisfies/schema.json diff --git a/factory/parser.ts b/factory/parser.ts index 212820f6f..43f8d7cb0 100644 --- a/factory/parser.ts +++ b/factory/parser.ts @@ -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; @@ -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()) diff --git a/src/NodeParser/SatisfiesNodeParser.ts b/src/NodeParser/SatisfiesNodeParser.ts new file mode 100644 index 000000000..effcef473 --- /dev/null +++ b/src/NodeParser/SatisfiesNodeParser.ts @@ -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); + } +} diff --git a/test/valid-data-type.test.ts b/test/valid-data-type.test.ts index a3372eeec..a55a0d8ba 100644 --- a/test/valid-data-type.test.ts +++ b/test/valid-data-type.test.ts @@ -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", "*")); }); diff --git a/test/valid-data/type-satisfies/main.ts b/test/valid-data/type-satisfies/main.ts new file mode 100644 index 000000000..bbf78575a --- /dev/null +++ b/test/valid-data/type-satisfies/main.ts @@ -0,0 +1,2 @@ +const record = { x: "hello", y: "goodbye" } satisfies Record; +export type MyType = keyof typeof record; diff --git a/test/valid-data/type-satisfies/schema.json b/test/valid-data/type-satisfies/schema.json new file mode 100644 index 000000000..85f6d2085 --- /dev/null +++ b/test/valid-data/type-satisfies/schema.json @@ -0,0 +1,13 @@ +{ + "$ref": "#/definitions/MyType", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "MyType": { + "enum": [ + "x", + "y" + ], + "type": "string" + } + } +}