-
Notifications
You must be signed in to change notification settings - Fork 18
TS type generation via aliases (#60) #61
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
9bbccad
669d33f
2f12469
e73461a
d0fc987
8e8d267
d9903da
00e4fc0
c28b4fd
6c2d18d
3f2afaa
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,18 @@ const configSchema = Type.Object( | |||||
| * The name of the dependency to import the Type from typebox | ||||||
| */ | ||||||
| typeboxImportDependencyName: Type.String({ default: "@sinclair/typebox" }), | ||||||
| /** | ||||||
| * Whether to generate TypeScript type aliases for each generated schema. | ||||||
| * | ||||||
| * When enabled, every `export const Foo = ...` also emits | ||||||
| * `export type Foo = UnwrapSchema<typeof Foo>`. | ||||||
| */ | ||||||
| generateTsTypes: Type.Boolean({ default: true }), | ||||||
| /** | ||||||
| * The symbol name used to unwrap a schema into a TypeScript type. | ||||||
| * Imported as a type from `typeboxImportDependencyName`. | ||||||
| */ | ||||||
| unwrapSchemaImportName: Type.String({ default: "Type.Static" }), | ||||||
|
||||||
| unwrapSchemaImportName: Type.String({ default: "Type.Static" }), | |
| unwrapSchemaImportName: Type.String({ default: "Static" }), |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,11 +27,25 @@ export type ProcessedModel = { | |
| function convertModelToStandalone( | ||
| input: Pick<ProcessedModel, "name" | "stringRepresentation">, | ||
| ) { | ||
| return `export const ${getConfig().exportedTypePrefix}${input.name} = ${input.stringRepresentation}\n`; | ||
| const generatedName = `${getConfig().exportedTypePrefix}${input.name}`; | ||
|
|
||
| let exportStr = `export const ${generatedName} = ${input.stringRepresentation}\n`; | ||
|
|
||
| if (getConfig().generateTsTypes) { | ||
| exportStr += `export type ${generatedName} = ${getConfig().unwrapSchemaImportName}<typeof ${generatedName}>\n` | ||
| } | ||
|
|
||
| return exportStr; | ||
| } | ||
|
|
||
| function typepoxImportStatement() { | ||
| return `import { ${getConfig().typeboxImportVariableName} } from "${ | ||
| let imports = getConfig().typeboxImportVariableName | ||
|
|
||
| if (getConfig().generateTsTypes) { | ||
| imports += `, type ${getConfig().unwrapSchemaImportName}`; | ||
| } | ||
|
Comment on lines
41
to
+46
|
||
|
|
||
| return `import { ${imports} } from "${ | ||
| getConfig().typeboxImportDependencyName | ||
| }"\n`; | ||
| } | ||
|
|
||
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.
The new config example introduces
unwrapSchemaImportName = "UnwrapSchema", but the actual default insrc/config.tsis currently "Type.Static" (which is not a valid named import). Please align the README with the real default and clarify thatunwrapSchemaImportNamemust be a named export identifier (e.g. "Static" for @sinclair/typebox) that can be imported withimport { type ... }fromtypeboxImportDependencyName.