-
Notifications
You must be signed in to change notification settings - Fork 0
feat(typebox): add support for keyof typeof expressions #18
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,90 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { BaseTypeHandler } from '@daxserver/validation-schema-codegen/handlers/typebox/base-type-handler' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { makeTypeCall } from '@daxserver/validation-schema-codegen/utils/typebox-codegen-utils' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Node, ts, TypeOperatorTypeNode, VariableDeclaration } from 'ts-morph' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| export class KeyOfTypeofHandler extends BaseTypeHandler { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| canHandle(node: Node): boolean { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Node.isTypeOperatorTypeNode(node) && | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| node.getOperator() === ts.SyntaxKind.KeyOfKeyword && | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Node.isTypeQuery(node.getTypeNode()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| handle(node: TypeOperatorTypeNode): ts.Expression { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const typeQuery = node.getTypeNode() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!Node.isTypeQuery(typeQuery)) return makeTypeCall('Any') | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| const exprName = typeQuery.getExprName() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!Node.isIdentifier(exprName)) return makeTypeCall('Any') | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| const keys = this.getObjectKeys(exprName) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return keys.length > 0 ? this.createUnion(keys) : makeTypeCall('Any') | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| private getObjectKeys(node: Node): string[] { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sourceFile = node.getSourceFile() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const varDecl of sourceFile.getVariableDeclarations()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (varDecl.getName() === node.getText()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this.extractKeys(varDecl) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| private extractKeys(varDecl: VariableDeclaration): string[] { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Try object literal | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| let initializer = varDecl.getInitializer() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Node.isAsExpression(initializer)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| initializer = initializer.getExpression() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Node.isObjectLiteralExpression(initializer)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return initializer | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .getProperties() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((prop) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Node.isPropertyAssignment(prop) || Node.isShorthandPropertyAssignment(prop)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const name = prop.getName() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return typeof name === 'string' ? name : null | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter((name): name is string => name !== null) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Try type annotation | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const typeNode = varDecl.getTypeNode() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Node.isTypeLiteral(typeNode)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return typeNode | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .getMembers() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((member) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Node.isPropertySignature(member)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const name = member.getName() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return typeof name === 'string' ? name : null | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter((name): name is string => name !== null) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| private createUnion(keys: string[]): ts.Expression { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const literals = keys.map((key) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const num = Number(key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const literal = | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| !isNaN(num) && key === String(num) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? ts.factory.createNumericLiteral(num) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| : ts.factory.createStringLiteral(key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return makeTypeCall('Literal', [literal]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return literals.length === 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? literals[0]! | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| : makeTypeCall('Union', [ts.factory.createArrayLiteralExpression(literals)]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+75
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect: numeric object keys should produce string literal key types. In TypeScript, property names are strings; Apply this diff to always emit string literals for object keys: - private createUnion(keys: string[]): ts.Expression {
- const literals = keys.map((key) => {
- const num = Number(key)
- const literal =
- !isNaN(num) && key === String(num)
- ? ts.factory.createNumericLiteral(num)
- : ts.factory.createStringLiteral(key)
-
- return makeTypeCall('Literal', [literal])
- })
+ private createUnion(keys: string[]): ts.Expression {
+ const literals = keys.map((key) => {
+ const literal = ts.factory.createStringLiteral(key)
+ return makeTypeCall('Literal', [literal])
+ })📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,123 @@ | ||||||||||||||||||||||||||||||||||||||
| import { createSourceFile, formatWithPrettier, generateFormattedCode } from '@test-fixtures/utils' | ||||||||||||||||||||||||||||||||||||||
| import { beforeEach, describe, expect, test } from 'bun:test' | ||||||||||||||||||||||||||||||||||||||
| import { Project } from 'ts-morph' | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| describe('KeyOf typeof handling', () => { | ||||||||||||||||||||||||||||||||||||||
| let project: Project | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| beforeEach(() => { | ||||||||||||||||||||||||||||||||||||||
| project = new Project() | ||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| test('should handle single string', () => { | ||||||||||||||||||||||||||||||||||||||
| const sourceFile = createSourceFile( | ||||||||||||||||||||||||||||||||||||||
| project, | ||||||||||||||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||||||||||||||
| const A = { | ||||||||||||||||||||||||||||||||||||||
| a: 'a', | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| type T = keyof typeof A | ||||||||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(generateFormattedCode(sourceFile)).toBe( | ||||||||||||||||||||||||||||||||||||||
| formatWithPrettier(` | ||||||||||||||||||||||||||||||||||||||
| export const T = Type.Literal("a"); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| export type T = Static<typeof T>; | ||||||||||||||||||||||||||||||||||||||
| `), | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| test('shoud handle single numeric', () => { | ||||||||||||||||||||||||||||||||||||||
| const sourceFile = createSourceFile( | ||||||||||||||||||||||||||||||||||||||
| project, | ||||||||||||||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||||||||||||||
| const A = { | ||||||||||||||||||||||||||||||||||||||
| 0: 'a', | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| type T = keyof typeof A | ||||||||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(generateFormattedCode(sourceFile)).toBe( | ||||||||||||||||||||||||||||||||||||||
| formatWithPrettier(` | ||||||||||||||||||||||||||||||||||||||
| export const T = Type.Literal(0); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| export type T = Static<typeof T>; | ||||||||||||||||||||||||||||||||||||||
| `), | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+43
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adjust expectation: numeric object keys are string key types.
- export const T = Type.Literal(0);
+ export const T = Type.Literal("0");📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| test('should handle multiple string properties', () => { | ||||||||||||||||||||||||||||||||||||||
| const sourceFile = createSourceFile( | ||||||||||||||||||||||||||||||||||||||
| project, | ||||||||||||||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||||||||||||||
| const A = { | ||||||||||||||||||||||||||||||||||||||
| a: 'a', | ||||||||||||||||||||||||||||||||||||||
| b: 'b', | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| type T = keyof typeof A | ||||||||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(generateFormattedCode(sourceFile)).toBe( | ||||||||||||||||||||||||||||||||||||||
| formatWithPrettier(` | ||||||||||||||||||||||||||||||||||||||
| export const T = Type.Union([ | ||||||||||||||||||||||||||||||||||||||
| Type.Literal("a"), | ||||||||||||||||||||||||||||||||||||||
| Type.Literal("b"), | ||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| export type T = Static<typeof T>; | ||||||||||||||||||||||||||||||||||||||
| `), | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| test('should handle multiple numeric properties', () => { | ||||||||||||||||||||||||||||||||||||||
| const sourceFile = createSourceFile( | ||||||||||||||||||||||||||||||||||||||
| project, | ||||||||||||||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||||||||||||||
| const A = { | ||||||||||||||||||||||||||||||||||||||
| 0: 'a', | ||||||||||||||||||||||||||||||||||||||
| 1: 'b', | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
| type T = keyof typeof A | ||||||||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(generateFormattedCode(sourceFile)).toBe( | ||||||||||||||||||||||||||||||||||||||
| formatWithPrettier(` | ||||||||||||||||||||||||||||||||||||||
| export const T = Type.Union([ | ||||||||||||||||||||||||||||||||||||||
| Type.Literal(0), | ||||||||||||||||||||||||||||||||||||||
| Type.Literal(1), | ||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| export type T = Static<typeof T>; | ||||||||||||||||||||||||||||||||||||||
| `), | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+88
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adjust expectation for multiple numeric keys: use strings. - export const T = Type.Union([
- Type.Literal(0),
- Type.Literal(1),
- ]);
+ export const T = Type.Union([
+ Type.Literal("0"),
+ Type.Literal("1"),
+ ]);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| test('should handle mixed properties', () => { | ||||||||||||||||||||||||||||||||||||||
| const sourceFile = createSourceFile( | ||||||||||||||||||||||||||||||||||||||
| project, | ||||||||||||||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||||||||||||||
| const A = { | ||||||||||||||||||||||||||||||||||||||
| a: 'a', | ||||||||||||||||||||||||||||||||||||||
| 0: 'b', | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
| type T = keyof typeof A | ||||||||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(generateFormattedCode(sourceFile)).toBe( | ||||||||||||||||||||||||||||||||||||||
| formatWithPrettier(` | ||||||||||||||||||||||||||||||||||||||
| export const T = Type.Union([ | ||||||||||||||||||||||||||||||||||||||
| Type.Literal("a"), | ||||||||||||||||||||||||||||||||||||||
| Type.Literal(0), | ||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+112
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adjust expectation for mixed keys: numeric key should be string literal. - export const T = Type.Union([
- Type.Literal("a"),
- Type.Literal(0),
- ]);
+ export const T = Type.Union([
+ Type.Literal("a"),
+ Type.Literal("0"),
+ ]);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| export type T = Static<typeof T>; | ||||||||||||||||||||||||||||||||||||||
| `), | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty objects should yield
Never, notAny.keyof {}isnever. Your current[] -> Anyconflates “resolved empty” with “unresolved”. ReturnAnyonly when the target couldn’t be resolved; returnNeverfor resolved-but-empty.Also applies to: 25-35
🤖 Prompt for AI Agents