Skip to content

Commit

Permalink
makeKeyOf() helper (#2231)
Browse files Browse the repository at this point in the history
Will reuse in #2214 and #2226
  • Loading branch information
RobinTail authored Dec 2, 2024
1 parent f5706d6 commit e0f0932
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
4 changes: 4 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ export default tsPlugin.config(
selector: "Identifier[name='EqualsEqualsEqualsToken']",
message: "use makeEqual() helper",
},
{
selector: "Identifier[name='KeyOfKeyword']",
message: "use makeKeyOf() helper",
},
],
},
},
Expand Down
11 changes: 7 additions & 4 deletions src/integration-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,20 @@ 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,
fallback: ts.TypeNode,
) =>
f.createConditionalTypeNode(
key,
f.createTypeOperatorNode(
ts.SyntaxKind.KeyOfKeyword,
f.createTypeReferenceNode(subject),
),
makeKeyOf(subject),
f.createIndexedAccessTypeNode(f.createTypeReferenceNode(subject), key),
fallback,
);
Expand Down
11 changes: 3 additions & 8 deletions src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
restToken,
makeAnd,
makeEqual,
makeKeyOf,
} from "./integration-helpers";
import { makeCleanId } from "./common-helpers";
import { Method, methods } from "./method";
Expand Down Expand Up @@ -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),
),
);

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e0f0932

Please sign in to comment.