Skip to content

Commit

Permalink
Fix refine with type guard signature
Browse files Browse the repository at this point in the history
  • Loading branch information
sbking committed Sep 11, 2022
1 parent 6011b67 commit b878546
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion deno/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
Expand Down
6 changes: 5 additions & 1 deletion deno/lib/__tests__/refine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof validationSchema>;
type Schema = z.infer<typeof validationSchema>;

util.assertEqual<"a", Input["a"]>(false);
util.assertEqual<string, Input["a"]>(true);

util.assertEqual<"a", Schema["a"]>(true);
util.assertEqual<"string", Schema["a"]>(false);
util.assertEqual<string, Schema["a"]>(false);
});

test("refinement Promise", async () => {
Expand Down
4 changes: 2 additions & 2 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export abstract class ZodType<
refine<RefinedOutput extends Output>(
check: (arg: Output) => arg is RefinedOutput,
message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)
): ZodEffects<this, RefinedOutput, RefinedOutput>;
): ZodEffects<this, RefinedOutput, Input>;
refine(
check: (arg: Output) => unknown | Promise<unknown>,
message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)
Expand Down Expand Up @@ -315,7 +315,7 @@ export abstract class ZodType<
refinement<RefinedOutput extends Output>(
check: (arg: Output) => arg is RefinedOutput,
refinementData: IssueData | ((arg: Output, ctx: RefinementCtx) => IssueData)
): ZodEffects<this, RefinedOutput, RefinedOutput>;
): ZodEffects<this, RefinedOutput, Input>;
refinement(
check: (arg: Output) => boolean,
refinementData: IssueData | ((arg: Output, ctx: RefinementCtx) => IssueData)
Expand Down
6 changes: 5 additions & 1 deletion src/__tests__/refine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof validationSchema>;
type Schema = z.infer<typeof validationSchema>;

util.assertEqual<"a", Input["a"]>(false);
util.assertEqual<string, Input["a"]>(true);

util.assertEqual<"a", Schema["a"]>(true);
util.assertEqual<"string", Schema["a"]>(false);
util.assertEqual<string, Schema["a"]>(false);
});

test("refinement Promise", async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export abstract class ZodType<
refine<RefinedOutput extends Output>(
check: (arg: Output) => arg is RefinedOutput,
message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)
): ZodEffects<this, RefinedOutput, RefinedOutput>;
): ZodEffects<this, RefinedOutput, Input>;
refine(
check: (arg: Output) => unknown | Promise<unknown>,
message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)
Expand Down Expand Up @@ -315,7 +315,7 @@ export abstract class ZodType<
refinement<RefinedOutput extends Output>(
check: (arg: Output) => arg is RefinedOutput,
refinementData: IssueData | ((arg: Output, ctx: RefinementCtx) => IssueData)
): ZodEffects<this, RefinedOutput, RefinedOutput>;
): ZodEffects<this, RefinedOutput, Input>;
refinement(
check: (arg: Output) => boolean,
refinementData: IssueData | ((arg: Output, ctx: RefinementCtx) => IssueData)
Expand Down

0 comments on commit b878546

Please sign in to comment.