Conversation
There was a problem hiding this comment.
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>>whengenerateTsTypesis 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" }), |
There was a problem hiding this comment.
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.
| unwrapSchemaImportName: Type.String({ default: "Type.Static" }), | |
| unwrapSchemaImportName: Type.String({ default: "Static" }), |
| function typepoxImportStatement() { | ||
| return `import { ${getConfig().typeboxImportVariableName} } from "${ | ||
| let imports = getConfig().typeboxImportVariableName | ||
|
|
||
| if (getConfig().generateTsTypes) { | ||
| imports += `, type ${getConfig().unwrapSchemaImportName}`; | ||
| } |
There was a problem hiding this comment.
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.
| // 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" |
There was a problem hiding this comment.
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.
| // 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" |
|
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. |
|
Hey, thanks for your contribution, please see #59 |
PR for #60