Skip to content

Commit 6be478b

Browse files
committed
Fix ZodType assignability issues
1 parent 6fd3b39 commit 6be478b

3 files changed

Lines changed: 49 additions & 13 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,17 @@ test("empty shape", () => {
503503
expect(() => a.parse([])).toThrow();
504504
expect(() => a.parse([], { jitless: true })).toThrow();
505505
});
506+
507+
test("zodtype assignability", () => {
508+
// Does not error
509+
z.object({ hello: z.string().optional() }) satisfies z.ZodType<{ hello?: string | undefined }>;
510+
z.object({ hello: z.string() }) satisfies z.ZodType<{ hello?: string | undefined }>;
511+
// @ts-expect-error
512+
z.object({}) satisfies z.ZodType<{ hello: string | undefined }>;
513+
// @ts-expect-error
514+
z.object({ hello: z.string().optional() }) satisfies z.ZodType<{ hello: string | undefined }>;
515+
// @ts-expect-error
516+
z.object({ hello: z.string().optional() }) satisfies z.ZodType<{ hello: string }>;
517+
// @ts-expect-error
518+
z.object({ hello: z.number() }) satisfies z.ZodType<{ hello?: string | undefined }>;
519+
});

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

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,6 @@ export interface $ZodTypeInternals<out O = unknown, out I = unknown> {
149149
parent?: $ZodType | undefined;
150150
}
151151

152-
// export interface $ZodTypeInternals<out O = unknown, out I = unknown> extends $ZodTypeInternals {
153-
// // "~types": { output: O; input: I };
154-
// output: O;
155-
// input: I;
156-
// }
157-
158-
// export interface _$ZodType {
159-
// _zod: $ZodTypeInternals;
160-
// "~standard": StandardSchemaV1.Props<core.input<this>, core.output<this>>;
161-
// }
162-
163152
export interface $ZodType<O = unknown, I = unknown> {
164153
_zod: $ZodTypeInternals<O, I>;
165154
"~standard": StandardSchemaV1.Props<core.input<this>, core.output<this>>;
@@ -1525,7 +1514,10 @@ export interface $ZodObjectInternals<
15251514
/** @ts-ignore Cast variance */
15261515
out Shape extends Readonly<$ZodShape> = Readonly<$ZodShape>,
15271516
out Config extends $ZodObjectConfig = $ZodObjectConfig,
1528-
> extends $ZodTypeInternals<any, any> {
1517+
> extends $ZodTypeInternals<
1518+
$InferObjectOutputFallback<Shape, Config["out"]>,
1519+
$InferObjectInputFallback<Shape, Config["in"]>
1520+
> {
15291521
def: $ZodObjectDef<Shape>;
15301522
config: Config;
15311523
isst: errors.$ZodIssueInvalidType | errors.$ZodIssueUnrecognizedKeys;
@@ -1541,6 +1533,36 @@ export type $ZodLooseShape = Record<string, any>;
15411533
type OptionalOutSchema = { _zod: { optout: "optional" } };
15421534
type OptionalInSchema = { _zod: { optin: "optional" } };
15431535

1536+
export type $InferObjectOutputFallback<
1537+
T extends $ZodLooseShape,
1538+
Extra extends Record<string, unknown>,
1539+
> = string extends keyof T
1540+
? object
1541+
: keyof (T & Extra) extends never
1542+
? Record<string, never>
1543+
: util.Prettify<
1544+
{
1545+
// this is a simplified fallback type
1546+
// there is no support for key optionality
1547+
-readonly [k in keyof T]: core.output<T[k]>;
1548+
} & Extra
1549+
>;
1550+
1551+
export type $InferObjectInputFallback<
1552+
T extends $ZodLooseShape,
1553+
Extra extends Record<string, unknown>,
1554+
> = string extends keyof T
1555+
? object
1556+
: keyof (T & Extra) extends never
1557+
? Record<string, never>
1558+
: util.Prettify<
1559+
{
1560+
// this is a simplified fallback type
1561+
// there is no support for key optionality
1562+
-readonly [k in keyof T]: core.input<T[k]>;
1563+
} & Extra
1564+
>;
1565+
15441566
export type $InferObjectOutput<T extends $ZodLooseShape, Extra extends Record<string, unknown>> = string extends keyof T
15451567
? object
15461568
: keyof (T & Extra) extends never

play.ts

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

33
z;

0 commit comments

Comments
 (0)