Skip to content

TS type generation via aliases (#60)#61

Open
hyoretsu wants to merge 11 commits intom1212e:mainfrom
hyoretsu:main
Open

TS type generation via aliases (#60)#61
hyoretsu wants to merge 11 commits intom1212e:mainfrom
hyoretsu:main

Conversation

@hyoretsu
Copy link
Copy Markdown

@hyoretsu hyoretsu commented Mar 29, 2026

PR for #60

Copilot AI review requested due to automatic review settings March 29, 2026 17:02
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds optional generation of TypeScript type aliases alongside the existing generated TypeBox schema constants, controlled via new generator configuration options.

Changes:

  • Emit export type <SchemaName> = <Unwrap><typeof <SchemaName>> when generateTsTypes is enabled.
  • Extend the generated import statement to additionally import the configured unwrap utility type.
  • Document the new generator options in the README.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/model.ts Adds optional TS type alias emission and conditionally imports the unwrap utility type.
src/config.ts Introduces generateTsTypes and unwrapSchemaImportName options in the config schema.
README.md Documents the new options in the generator configuration example.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

src/config.ts Outdated
* The symbol name used to unwrap a schema into a TypeScript type.
* Imported as a type from `typeboxImportDependencyName`.
*/
unwrapSchemaImportName: Type.String({ default: "Type.Static" }),
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrapSchemaImportName defaults to "Type.Static", but the generator treats this as a named type import (and as a generic type in the emitted alias). A dotted path like Type.Static cannot be imported in import { ..., type X } from ... and will produce invalid generated TypeScript by default (especially since generateTsTypes defaults to true). Change the default to a valid exported type identifier (e.g. "Static" for @sinclair/typebox), or adjust the generator/config to support dotted type expressions without importing them.

Suggested change
unwrapSchemaImportName: Type.String({ default: "Type.Static" }),
unwrapSchemaImportName: Type.String({ default: "Static" }),

Copilot uses AI. Check for mistakes.
Comment on lines 41 to +46
function typepoxImportStatement() {
return `import { ${getConfig().typeboxImportVariableName} } from "${
let imports = getConfig().typeboxImportVariableName

if (getConfig().generateTsTypes) {
imports += `, type ${getConfig().unwrapSchemaImportName}`;
}
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typepoxImportStatement() and convertModelToStandalone() assume unwrapSchemaImportName is a valid named import specifier and generic type identifier. With the current config default ("Type.Static"), this would emit import { ..., type Type.Static } ... and Type.Static<typeof X>, which is invalid TS. Consider validating unwrapSchemaImportName (must be an identifier) and/or supporting a separate config for the type expression vs the type import name so dotted paths don’t break codegen.

Copilot uses AI. Check for mistakes.
Comment on lines +25 to +28
// optionally generate TS aliases for each schema: export type MySchema = UnwrapSchema<typeof MySchema>
generateTsTypes = true
// optionally customize the unwrap utility type symbol (imported from the same package as typeboxImportDependencyName)
unwrapSchemaImportName = "UnwrapSchema"
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new config example introduces unwrapSchemaImportName = "UnwrapSchema", but the actual default in src/config.ts is currently "Type.Static" (which is not a valid named import). Please align the README with the real default and clarify that unwrapSchemaImportName must be a named export identifier (e.g. "Static" for @sinclair/typebox) that can be imported with import { type ... } from typeboxImportDependencyName.

Suggested change
// optionally generate TS aliases for each schema: export type MySchema = UnwrapSchema<typeof MySchema>
generateTsTypes = true
// optionally customize the unwrap utility type symbol (imported from the same package as typeboxImportDependencyName)
unwrapSchemaImportName = "UnwrapSchema"
// optionally generate TS aliases for each schema: export type MySchema = Static<typeof MySchema>
generateTsTypes = true
// optionally customize the unwrap utility type symbol (must be a named export identifier that can be imported with:
// import { type <unwrapSchemaImportName> } from typeboxImportDependencyName; defaults to "Type.Static" for @sinclair/typebox)
unwrapSchemaImportName = "Static"

Copilot uses AI. Check for mistakes.
@hyoretsu
Copy link
Copy Markdown
Author

I tested it locally with the backend I was migrating and it works just fine, a great DX since I can't "export *" from prisma-kysely which exports TS types.

@m1212e
Copy link
Copy Markdown
Owner

m1212e commented Mar 30, 2026

Hey, thanks for your contribution, please see #59
Feel free to post your fork over there so people will find it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants