From e0f0932fe86a0ec89328785e7face43f36e251e4 Mon Sep 17 00:00:00 2001 From: Anna Bocharova Date: Mon, 2 Dec 2024 21:13:06 +0100 Subject: [PATCH] `makeKeyOf()` helper (#2231) Will reuse in #2214 and #2226 --- eslint.config.js | 4 ++++ src/integration-helpers.ts | 11 +++++++---- src/integration.ts | 11 +++-------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index 57cc5c94e..dd71a9b0f 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -140,6 +140,10 @@ export default tsPlugin.config( selector: "Identifier[name='EqualsEqualsEqualsToken']", message: "use makeEqual() helper", }, + { + selector: "Identifier[name='KeyOfKeyword']", + message: "use makeKeyOf() helper", + }, ], }, }, diff --git a/src/integration-helpers.ts b/src/integration-helpers.ts index a4c5d368e..e7b2f26fa 100644 --- a/src/integration-helpers.ts +++ b/src/integration-helpers.ts @@ -139,6 +139,12 @@ export const makePublicClass = ( ...statements, ]); +export const makeKeyOf = (id: ts.Identifier) => + f.createTypeOperatorNode( + ts.SyntaxKind.KeyOfKeyword, + f.createTypeReferenceNode(id), + ); + export const makeConditionalIndex = ( subject: ts.Identifier, key: ts.TypeNode, @@ -146,10 +152,7 @@ export const makeConditionalIndex = ( ) => f.createConditionalTypeNode( key, - f.createTypeOperatorNode( - ts.SyntaxKind.KeyOfKeyword, - f.createTypeReferenceNode(subject), - ), + makeKeyOf(subject), f.createIndexedAccessTypeNode(f.createTypeReferenceNode(subject), key), fallback, ); diff --git a/src/integration.ts b/src/integration.ts index 2aa831452..56ce0214b 100644 --- a/src/integration.ts +++ b/src/integration.ts @@ -31,6 +31,7 @@ import { restToken, makeAnd, makeEqual, + makeKeyOf, } from "./integration-helpers"; import { makeCleanId } from "./common-helpers"; import { Method, methods } from "./method"; @@ -292,10 +293,7 @@ export class Integration { this.program.push( makePublicType( this.ids.methodPathType, - f.createTypeOperatorNode( - ts.SyntaxKind.KeyOfKeyword, - f.createTypeReferenceNode(this.ids.inputInterface), - ), + makeKeyOf(this.ids.inputInterface), ), ); @@ -402,10 +400,7 @@ export class Integration { [this.ids.pathParameter.text]: f.createTypeReferenceNode("P"), [this.ids.paramsArgument.text]: f.createConditionalTypeNode( parametricIndexNode, - f.createTypeOperatorNode( - ts.SyntaxKind.KeyOfKeyword, - f.createTypeReferenceNode(this.ids.inputInterface), - ), + makeKeyOf(this.ids.inputInterface), f.createIndexedAccessTypeNode( f.createTypeReferenceNode(this.ids.inputInterface), parametricIndexNode,