Skip to content

Commit 2be1c6a

Browse files
committed
Fix generic assignability issue. 3.25.54
1 parent 8ab2374 commit 2be1c6a

File tree

5 files changed

+36
-37
lines changed

5 files changed

+36
-37
lines changed

packages/zod/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zod",
3-
"version": "3.25.53",
3+
"version": "3.25.54",
44
"type": "module",
55
"author": "Colin McDonnell <[email protected]>",
66
"description": "TypeScript-first schema declaration and validation library with static type inference",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export interface ZodType<
7272
nonoptional(params?: string | core.$ZodNonOptionalParams): ZodNonOptional<this>;
7373
nullable(): ZodNullable<this>;
7474
nullish(): ZodOptional<ZodNullable<this>>;
75-
default(def: util.NoUndefined<core.output<this>>): ZodDefault<this>;
75+
default(def: core.output<this>): ZodDefault<this>;
7676
default(def: () => util.NoUndefined<core.output<this>>): ZodDefault<this>;
7777
prefault(def: () => core.input<this>): ZodPrefault<this>;
7878
prefault(def: core.input<this>): ZodPrefault<this>;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,19 @@ test("assignability with narrowing", () => {
192192

193193
type RefinedUnionSchema<T extends z.ZodUnion> = T;
194194
});
195+
196+
test("generic assignability in objects", () => {
197+
interface SortItem<T extends string> {
198+
key: T;
199+
order: string;
200+
}
201+
202+
const createSortItemSchema = <T extends z.ZodType<string>>(sortKeySchema: T) =>
203+
z.object({
204+
key: sortKeySchema,
205+
order: z.string(),
206+
});
207+
208+
<T extends z.ZodType<string>>(sortKeySchema: T, defaultSortBy: SortItem<z.output<T>>[] = []) =>
209+
createSortItemSchema(sortKeySchema).array().default(defaultSortBy);
210+
});

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

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,45 +1527,15 @@ export const $ZodArray: core.$constructor<$ZodArray> = /*@__PURE__*/ core.$const
15271527
type OptionalOutSchema = { _zod: { optout: "optional" } };
15281528
type OptionalInSchema = { _zod: { optin: "optional" } };
15291529

1530-
export type $InferObjectOutputFallback<
1531-
T extends $ZodLooseShape,
1532-
Extra extends Record<string, unknown>,
1533-
> = string extends keyof T
1534-
? object
1535-
: keyof (T & Extra) extends never
1536-
? Record<string, never>
1537-
: util.Prettify<
1538-
{
1539-
// this is a simplified fallback type
1540-
// there is no support for key optionality
1541-
-readonly [k in keyof T]: core.output<T[k]>;
1542-
} & Extra
1543-
>;
1544-
1545-
export type $InferObjectInputFallback<
1546-
T extends $ZodLooseShape,
1547-
Extra extends Record<string, unknown>,
1548-
> = string extends keyof T
1549-
? object
1550-
: keyof (T & Extra) extends never
1551-
? Record<string, never>
1552-
: util.Prettify<
1553-
{
1554-
// this is a simplified fallback type
1555-
// there is no support for key optionality
1556-
-readonly [k in keyof T]: core.input<T[k]>;
1557-
} & Extra
1558-
>;
1559-
15601530
export type $InferObjectOutput<T extends $ZodLooseShape, Extra extends Record<string, unknown>> = string extends keyof T
15611531
? object
15621532
: keyof (T & Extra) extends never
15631533
? Record<string, never>
15641534
: util.Prettify<
15651535
{
1566-
-readonly [k in keyof T as T[k] extends OptionalOutSchema ? never : k]: core.output<T[k]>;
1536+
-readonly [k in keyof T as T[k] extends OptionalOutSchema ? never : k]: T[k]["_zod"]["output"];
15671537
} & {
1568-
-readonly [k in keyof T as T[k] extends OptionalOutSchema ? k : never]?: core.output<T[k]>;
1538+
-readonly [k in keyof T as T[k] extends OptionalOutSchema ? k : never]?: T[k]["_zod"]["output"];
15691539
} & Extra
15701540
>;
15711541

@@ -1575,9 +1545,9 @@ export type $InferObjectInput<T extends $ZodLooseShape, Extra extends Record<str
15751545
? Record<string, never>
15761546
: util.Prettify<
15771547
{
1578-
-readonly [k in keyof T as T[k] extends OptionalInSchema ? never : k]: core.input<T[k]>;
1548+
-readonly [k in keyof T as T[k] extends OptionalInSchema ? never : k]: T[k]["_zod"]["input"];
15791549
} & {
1580-
-readonly [k in keyof T as T[k] extends OptionalInSchema ? k : never]?: core.input<T[k]>;
1550+
-readonly [k in keyof T as T[k] extends OptionalInSchema ? k : never]?: T[k]["_zod"]["input"];
15811551
} & Extra
15821552
>;
15831553

@@ -1874,6 +1844,7 @@ export const $ZodObject: core.$constructor<$ZodObject> = /*@__PURE__*/ core.$con
18741844
////////// ///////////
18751845
/////////////////////////////////////////
18761846
/////////////////////////////////////////
1847+
// use generic to distribute union types
18771848
export type $InferUnionOutput<T extends SomeType> = T extends any ? core.output<T> : never;
18781849
export type $InferUnionInput<T extends SomeType> = T extends any ? core.input<T> : never;
18791850
export interface $ZodUnionDef<Options extends readonly SomeType[] = readonly $ZodType[]> extends $ZodTypeDef {

play.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
import { z } from "zod/v4";
22

3-
z;
3+
export interface SortItem<T extends string> {
4+
key: T;
5+
order: string;
6+
}
7+
8+
export const createSortItemSchema = <T extends z.ZodType<string>>(sortKeySchema: T) =>
9+
z.object({
10+
key: sortKeySchema,
11+
order: z.string(),
12+
});
13+
14+
const error = <T extends z.ZodType<string>>(sortKeySchema: T, defaultSortBy: SortItem<z.output<T>>[] = []) =>
15+
createSortItemSchema(sortKeySchema).array().default(defaultSortBy);

0 commit comments

Comments
 (0)