Skip to content
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

Fix refine with type guard signature #1404

Merged
merged 1 commit into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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