Skip to content

Commit a3e4391

Browse files
authored
Unwiden catch input type (#4870)
1 parent bdefa16 commit a3e4391

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

packages/zod/src/v4/classic/tests/catch.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { expect, expectTypeOf, test } from "vitest";
22
import { z } from "zod/v4";
3-
import type { util } from "zod/v4/core";
43

54
test("basic catch", () => {
65
expect(z.string().catch("default").parse(undefined)).toBe("default");
@@ -45,7 +44,7 @@ test("catch with transform", () => {
4544
expect(stringWithDefault.unwrap().out).toBeInstanceOf(z.ZodTransform);
4645

4746
type inp = z.input<typeof stringWithDefault>;
48-
expectTypeOf<inp>().toEqualTypeOf<string | util.Whatever>();
47+
expectTypeOf<inp>().toEqualTypeOf<string>();
4948
type out = z.output<typeof stringWithDefault>;
5049
expectTypeOf<out>().toEqualTypeOf<string>();
5150
});
@@ -59,7 +58,7 @@ test("catch on existing optional", () => {
5958
expect(stringWithDefault.unwrap().unwrap()).toBeInstanceOf(z.ZodString);
6059

6160
type inp = z.input<typeof stringWithDefault>;
62-
expectTypeOf<inp>().toEqualTypeOf<string | undefined | util.Whatever>();
61+
expectTypeOf<inp>().toEqualTypeOf<string | undefined>();
6362
type out = z.output<typeof stringWithDefault>;
6463
expectTypeOf<out>().toEqualTypeOf<string | undefined>();
6564
});
@@ -68,7 +67,7 @@ test("optional on catch", () => {
6867
const stringWithDefault = z.string().catch("asdf").optional();
6968

7069
type inp = z.input<typeof stringWithDefault>;
71-
expectTypeOf<inp>().toEqualTypeOf<string | util.Whatever>();
70+
expectTypeOf<inp>().toEqualTypeOf<string | undefined>();
7271
type out = z.output<typeof stringWithDefault>;
7372
expectTypeOf<out>().toEqualTypeOf<string | undefined>();
7473
});
@@ -102,7 +101,7 @@ test("nested", () => {
102101
inner: "asdf",
103102
});
104103
type input = z.input<typeof outer>;
105-
expectTypeOf<input>().toEqualTypeOf<{ inner: string | util.Whatever } | util.Whatever>();
104+
expectTypeOf<input>().toEqualTypeOf<{ inner: string }>();
106105
type out = z.output<typeof outer>;
107106

108107
expectTypeOf<out>().toEqualTypeOf<{ inner: string }>();

packages/zod/src/v4/core/schemas.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3315,11 +3315,8 @@ export interface $ZodCatchDef<T extends SomeType = $ZodType> extends $ZodTypeDef
33153315
}
33163316

33173317
export interface $ZodCatchInternals<T extends SomeType = $ZodType>
3318-
extends $ZodTypeInternals<core.output<T>, core.input<T> | util.Whatever> {
3318+
extends $ZodTypeInternals<core.output<T>, core.input<T>> {
33193319
def: $ZodCatchDef<T>;
3320-
// qin: T["_zod"]["qin"];
3321-
// qout: T["_zod"]["qout"];
3322-
33233320
optin: T["_zod"]["optin"];
33243321
optout: T["_zod"]["optout"];
33253322
isst: never;

play.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import { z } from "zod";
22

33
z;
4+
5+
z.string().catch("asdf");

0 commit comments

Comments
 (0)