From b49a9b84513f5ccccd12357cdb474bd0272244bc Mon Sep 17 00:00:00 2001 From: Isaac Way Date: Sat, 14 Jan 2023 12:03:42 -0600 Subject: [PATCH 1/2] fix typing issues with refine and transform usage --- src/__tests__/createSchemaForm.test.tsx | 18 ++++++++++++------ src/createSchemaForm.tsx | 10 ++++++---- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/__tests__/createSchemaForm.test.tsx b/src/__tests__/createSchemaForm.test.tsx index 3b87638..0366673 100644 --- a/src/__tests__/createSchemaForm.test.tsx +++ b/src/__tests__/createSchemaForm.test.tsx @@ -767,7 +767,8 @@ describe("createSchemaForm", () => { render(
{}} />); }).toThrow(); }); - it("should correctly type the form with multiple unique field schemas.", () => { + + it("should have correct typings with multiple unique field schemas when transform and refine are used.", () => { const A = createUniqueFieldSchema(z.string(), "one"); const B = createUniqueFieldSchema(z.string(), "two"); function In1(_: { req: string }) { @@ -784,11 +785,16 @@ describe("createSchemaForm", () => { const Form = createTsForm(mapping); {}} + schema={z + .object({ + a: A, + b: B, + }) + .refine((_) => true) + .transform((a) => a.a)} + onSubmit={(data) => { + data.startsWith("cool"); // just type checks as a string + }} props={{ a: { req: "One", diff --git a/src/createSchemaForm.tsx b/src/createSchemaForm.tsx index 333b624..23d31b1 100644 --- a/src/createSchemaForm.tsx +++ b/src/createSchemaForm.tsx @@ -90,8 +90,10 @@ export type ExtraProps = { type UnwrapEffects> = T extends AnyZodObject ? T - : T extends ZodEffects - ? T["_def"]["schema"] + : T extends ZodEffects + ? EffectsSchema extends ZodEffects + ? EffectsSchemaInner + : EffectsSchema : never; function checkForDuplicateTypes(array: RTFSupportedZodTypes[]) { @@ -237,7 +239,7 @@ export function createTsForm< /** * A callback function that will be called with the data once the form has been submitted and validated successfully. */ - onSubmit: (values: z.infer>) => void; + onSubmit: (values: z.infer) => void; /** * Initializes your form with default values. Is a deep partial, so all properties and nested properties are optional. */ @@ -296,7 +298,7 @@ export function createTsForm< */ props?: RequireKeysWithRequiredChildren< Partial<{ - [key in keyof z.infer]: Mapping[IndexOf< + [key in keyof z.infer>]: Mapping[IndexOf< UnwrapMapping, readonly [ IndexOfUnwrapZodType< From a1f02371843dea25ab2ce4543c31f6b172a86352 Mon Sep 17 00:00:00 2001 From: Isaac Way Date: Sat, 14 Jan 2023 12:06:58 -0600 Subject: [PATCH 2/2] update test to use expectTypeOf --- src/__tests__/createSchemaForm.test.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/__tests__/createSchemaForm.test.tsx b/src/__tests__/createSchemaForm.test.tsx index 0366673..4ba342c 100644 --- a/src/__tests__/createSchemaForm.test.tsx +++ b/src/__tests__/createSchemaForm.test.tsx @@ -22,6 +22,7 @@ import { useReqDescription, useTsController, } from "../FieldContext"; +import { expectTypeOf } from "expect-type"; import { createUniqueFieldSchema } from "../createFieldSchema"; const testIds = { @@ -793,7 +794,7 @@ describe("createSchemaForm", () => { .refine((_) => true) .transform((a) => a.a)} onSubmit={(data) => { - data.startsWith("cool"); // just type checks as a string + expectTypeOf(data).toBeString(); }} props={{ a: {