From d3981407d95e8fd91c144499f6c933f6f3c084a7 Mon Sep 17 00:00:00 2001 From: romeerez Date: Sat, 2 Dec 2023 17:06:35 -0500 Subject: [PATCH] Fix schema-to-zod unability to infer schema type (#219) --- .changeset/short-cooks-help.md | 5 +++++ packages/schema-to-zod/src/main.test.ts | 1 + packages/schema-to-zod/src/main.ts | 9 +++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 .changeset/short-cooks-help.md diff --git a/.changeset/short-cooks-help.md b/.changeset/short-cooks-help.md new file mode 100644 index 000000000..f23323fc7 --- /dev/null +++ b/.changeset/short-cooks-help.md @@ -0,0 +1,5 @@ +--- +'orchid-orm-schema-to-zod': patch +--- + +Fix schema-to-zod unability to infer schema type diff --git a/packages/schema-to-zod/src/main.test.ts b/packages/schema-to-zod/src/main.test.ts index e79c78243..c15338bd2 100644 --- a/packages/schema-to-zod/src/main.test.ts +++ b/packages/schema-to-zod/src/main.test.ts @@ -69,6 +69,7 @@ describe('zodSchemaProvider', () => { columns, }; } + columns!: typeof columns; } const schema = Table.schema(); diff --git a/packages/schema-to-zod/src/main.ts b/packages/schema-to-zod/src/main.ts index 0d0c42a1b..8026ed399 100644 --- a/packages/schema-to-zod/src/main.ts +++ b/packages/schema-to-zod/src/main.ts @@ -220,7 +220,12 @@ type MapJsonTuple = T extends [infer Head, ...infer Tail] type Columns = { shape: ColumnsShapeBase }; type Table = { columns: ColumnsShapeBase }; -type TableClass = { instance(): T }; + +type TableClass = { + new (): T; + // Cannot set T as the `instance()` return type because it cannot be properly inferred + instance(): unknown; +}; export type InstanceToZod = z.ZodObject<{ [K in keyof T['shape']]: SchemaToZod; @@ -244,7 +249,7 @@ export const zodSchemaProvider = function ( this: TableClass, ): InstanceToZod<{ shape: T['columns'] }> { return instanceToZod({ - shape: this.instance().columns, + shape: (this.instance() as T).columns, }) as unknown as InstanceToZod<{ shape: T['columns']; }>;