From 0da77dac179c4b9cfe966432a75665f02869f16e Mon Sep 17 00:00:00 2001 From: Stephen Brian King <3913213+sbking@users.noreply.github.com> Date: Sun, 11 Sep 2022 10:52:11 -0600 Subject: [PATCH] Fix refine with type guard signature --- deno/lib/README.md | 2 +- deno/lib/__tests__/refine.test.ts | 6 +++++- deno/lib/types.ts | 2 +- src/__tests__/refine.test.ts | 6 +++++- src/types.ts | 2 +- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/deno/lib/README.md b/deno/lib/README.md index 2b6c41743a..856f8c6330 100644 --- a/deno/lib/README.md +++ b/deno/lib/README.md @@ -604,7 +604,7 @@ dateSchema.safeParse(new Date("1/12/22")); // success: true dateSchema.safeParse("2022-01-12T00:00:00.000Z"); // success: true ``` -## Zod enums- +## Zod enums ```ts const FishEnum = z.enum(["Salmon", "Tuna", "Trout"]); diff --git a/deno/lib/__tests__/refine.test.ts b/deno/lib/__tests__/refine.test.ts index b5e83c27f7..b0cee3a6b2 100644 --- a/deno/lib/__tests__/refine.test.ts +++ b/deno/lib/__tests__/refine.test.ts @@ -54,10 +54,14 @@ test("refinement type guard", () => { const validationSchema = z.object({ a: z.string().refine((s): s is "a" => s === "a"), }); + type Input = z.input; type Schema = z.infer; + util.assertEqual<"a", Input["a"]>(false); + util.assertEqual(true); + util.assertEqual<"a", Schema["a"]>(true); - util.assertEqual<"string", Schema["a"]>(false); + util.assertEqual(false); }); test("refinement Promise", async () => { diff --git a/deno/lib/types.ts b/deno/lib/types.ts index 13f6eaaa91..51a9180c04 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -268,7 +268,7 @@ export abstract class ZodType< refine( check: (arg: Output) => arg is RefinedOutput, message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams) - ): ZodEffects; + ): ZodEffects; refine( check: (arg: Output) => unknown | Promise, message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams) diff --git a/src/__tests__/refine.test.ts b/src/__tests__/refine.test.ts index 52f484c147..1fd8022e17 100644 --- a/src/__tests__/refine.test.ts +++ b/src/__tests__/refine.test.ts @@ -53,10 +53,14 @@ test("refinement type guard", () => { const validationSchema = z.object({ a: z.string().refine((s): s is "a" => s === "a"), }); + type Input = z.input; type Schema = z.infer; + util.assertEqual<"a", Input["a"]>(false); + util.assertEqual(true); + util.assertEqual<"a", Schema["a"]>(true); - util.assertEqual<"string", Schema["a"]>(false); + util.assertEqual(false); }); test("refinement Promise", async () => { diff --git a/src/types.ts b/src/types.ts index 1d4badcdd6..ee8ef8a2eb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -268,7 +268,7 @@ export abstract class ZodType< refine( check: (arg: Output) => arg is RefinedOutput, message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams) - ): ZodEffects; + ): ZodEffects; refine( check: (arg: Output) => unknown | Promise, message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)