diff --git a/deno/lib/__tests__/object.test.ts b/deno/lib/__tests__/object.test.ts index e3e161038..ba49e6339 100644 --- a/deno/lib/__tests__/object.test.ts +++ b/deno/lib/__tests__/object.test.ts @@ -229,7 +229,7 @@ test("inferred merged object type with optional properties", async () => { .object({ a: z.string(), b: z.string().optional() }) .merge(z.object({ a: z.string().optional(), b: z.string() })); type Merged = z.infer; - util.assertEqual(true); + util.assertEqual(true); // todo // util.assertEqual(true); }); diff --git a/deno/lib/types.ts b/deno/lib/types.ts index b96af8059..420238114 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -2119,43 +2119,26 @@ export class ZodObject< */ nonstrict = this.passthrough; - // augment = AugmentFactory>(this._def); - // extend = AugmentFactory>(this._def); - extend< - Augmentation extends ZodRawShape, - // NewShape extends extendShape, - // OldOutput = util.flatten>, - // AugOutput = baseObjectOutputType, - // NewOutput = OldOutput & AugOutput, - NewOutput extends util.flatten<{ - [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation - ? Augmentation[k]["_output"] - : k extends keyof Output - ? Output[k] - : never; - }>, - // OldInput = util.flatten>, - // AugInput = baseObjectInputType, - // NewInput = OldInput & AugInput - NewInput extends util.flatten<{ - [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation - ? Augmentation[k]["_input"] - : k extends keyof Input - ? Input[k] - : never; - }> - // AKeys extends string | number | symbol = keyof Augmentation, - - // AKeys extends string | number | symbol = keyof Augmentation - >( + // const AugmentFactory = + // (def: Def) => + // ( + // augmentation: Augmentation + // ): ZodObject< + // extendShape, Augmentation>, + // Def["unknownKeys"], + // Def["catchall"] + // > => { + // return new ZodObject({ + // ...def, + // shape: () => ({ + // ...def.shape(), + // ...augmentation, + // }), + // }) as any; + // }; + extend( augmentation: Augmentation - ): ZodObject< - extendShape, - UnknownKeys, - Catchall, - NewOutput, - NewInput - > { + ): ZodObject, UnknownKeys, Catchall> { return new ZodObject({ ...this._def, shape: () => ({ @@ -2164,6 +2147,39 @@ export class ZodObject< }), }) as any; } + // extend< + // Augmentation extends ZodRawShape, + // NewOutput extends util.flatten<{ + // [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation + // ? Augmentation[k]["_output"] + // : k extends keyof Output + // ? Output[k] + // : never; + // }>, + // NewInput extends util.flatten<{ + // [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation + // ? Augmentation[k]["_input"] + // : k extends keyof Input + // ? Input[k] + // : never; + // }> + // >( + // augmentation: Augmentation + // ): ZodObject< + // extendShape, + // UnknownKeys, + // Catchall, + // NewOutput, + // NewInput + // > { + // return new ZodObject({ + // ...this._def, + // shape: () => ({ + // ...this._def.shape(), + // ...augmentation, + // }), + // }) as any; + // } /** * @deprecated Use `.extend` instead * */ @@ -2174,49 +2190,13 @@ export class ZodObject< * inferred type of merged objects. Please * upgrade if you are experiencing issues. */ - // merge(merging: Incoming) { - // return this.extend(merging.shape as Incoming["shape"]); - // } - merge< - Incoming extends AnyZodObject, - Augmentation extends Incoming["shape"], - // NewShape extends extendShape, - // OldOutput = util.flatten>, - // AugOutput = baseObjectOutputType, - // NewOutput = OldOutput & AugOutput, - NewOutput extends { - [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation - ? Augmentation[k]["_output"] - : k extends keyof Output - ? Output[k] - : never; - }, - // OldInput = util.flatten>, - // AugInput = baseObjectInputType, - // NewInput = OldInput & AugInput - NewInput extends { - [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation - ? Augmentation[k]["_input"] - : k extends keyof Input - ? Input[k] - : never; - } - // AKeys extends string | number | symbol = keyof Augmentation, - - // AKeys extends string | number | symbol = keyof Augmentation - >( + merge( merging: Incoming ): ZodObject< - extendShape>, + extendShape, Incoming["_def"]["unknownKeys"], - Incoming["_def"]["catchall"], - NewOutput, - NewInput + Incoming["_def"]["catchall"] > { - // const mergedShape = objectUtil.mergeShapes( - // this._def.shape(), - // merging._def.shape() - // ); const merged: any = new ZodObject({ unknownKeys: merging._def.unknownKeys, catchall: merging._def.catchall, @@ -2226,6 +2206,41 @@ export class ZodObject< }) as any; return merged; } + // merge< + // Incoming extends AnyZodObject, + // Augmentation extends Incoming["shape"], + // NewOutput extends { + // [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation + // ? Augmentation[k]["_output"] + // : k extends keyof Output + // ? Output[k] + // : never; + // }, + // NewInput extends { + // [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation + // ? Augmentation[k]["_input"] + // : k extends keyof Input + // ? Input[k] + // : never; + // } + // >( + // merging: Incoming + // ): ZodObject< + // extendShape>, + // Incoming["_def"]["unknownKeys"], + // Incoming["_def"]["catchall"], + // NewOutput, + // NewInput + // > { + // const merged: any = new ZodObject({ + // unknownKeys: merging._def.unknownKeys, + // catchall: merging._def.catchall, + // shape: () => + // objectUtil.mergeShapes(this._def.shape(), merging._def.shape()), + // typeName: ZodFirstPartyTypeKind.ZodObject, + // }) as any; + // return merged; + // } setKey( key: Key, diff --git a/package.json b/package.json index 5ea82be17..a2fc21f48 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zod", - "version": "3.20.5", + "version": "3.20.6", "description": "TypeScript-first schema declaration and validation library with static type inference", "main": "./lib/index.js", "types": "./index.d.ts", diff --git a/playground.ts b/playground.ts index 06eab0c20..73b6acd67 100644 --- a/playground.ts +++ b/playground.ts @@ -1,4 +1,6 @@ -// import { z } from "./src"; +import { z } from "./src"; + +z.number().int(); // const arg = z.enum(["a", "b"] as const); // const arb = arg.exclude(["b"]); diff --git a/src/__tests__/object.test.ts b/src/__tests__/object.test.ts index 67b3e4979..41c712c7b 100644 --- a/src/__tests__/object.test.ts +++ b/src/__tests__/object.test.ts @@ -228,7 +228,7 @@ test("inferred merged object type with optional properties", async () => { .object({ a: z.string(), b: z.string().optional() }) .merge(z.object({ a: z.string().optional(), b: z.string() })); type Merged = z.infer; - util.assertEqual(true); + util.assertEqual(true); // todo // util.assertEqual(true); }); diff --git a/src/types.ts b/src/types.ts index 98dbd397a..c0c057f05 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2119,43 +2119,26 @@ export class ZodObject< */ nonstrict = this.passthrough; - // augment = AugmentFactory>(this._def); - // extend = AugmentFactory>(this._def); - extend< - Augmentation extends ZodRawShape, - // NewShape extends extendShape, - // OldOutput = util.flatten>, - // AugOutput = baseObjectOutputType, - // NewOutput = OldOutput & AugOutput, - NewOutput extends util.flatten<{ - [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation - ? Augmentation[k]["_output"] - : k extends keyof Output - ? Output[k] - : never; - }>, - // OldInput = util.flatten>, - // AugInput = baseObjectInputType, - // NewInput = OldInput & AugInput - NewInput extends util.flatten<{ - [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation - ? Augmentation[k]["_input"] - : k extends keyof Input - ? Input[k] - : never; - }> - // AKeys extends string | number | symbol = keyof Augmentation, - - // AKeys extends string | number | symbol = keyof Augmentation - >( + // const AugmentFactory = + // (def: Def) => + // ( + // augmentation: Augmentation + // ): ZodObject< + // extendShape, Augmentation>, + // Def["unknownKeys"], + // Def["catchall"] + // > => { + // return new ZodObject({ + // ...def, + // shape: () => ({ + // ...def.shape(), + // ...augmentation, + // }), + // }) as any; + // }; + extend( augmentation: Augmentation - ): ZodObject< - extendShape, - UnknownKeys, - Catchall, - NewOutput, - NewInput - > { + ): ZodObject, UnknownKeys, Catchall> { return new ZodObject({ ...this._def, shape: () => ({ @@ -2164,6 +2147,39 @@ export class ZodObject< }), }) as any; } + // extend< + // Augmentation extends ZodRawShape, + // NewOutput extends util.flatten<{ + // [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation + // ? Augmentation[k]["_output"] + // : k extends keyof Output + // ? Output[k] + // : never; + // }>, + // NewInput extends util.flatten<{ + // [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation + // ? Augmentation[k]["_input"] + // : k extends keyof Input + // ? Input[k] + // : never; + // }> + // >( + // augmentation: Augmentation + // ): ZodObject< + // extendShape, + // UnknownKeys, + // Catchall, + // NewOutput, + // NewInput + // > { + // return new ZodObject({ + // ...this._def, + // shape: () => ({ + // ...this._def.shape(), + // ...augmentation, + // }), + // }) as any; + // } /** * @deprecated Use `.extend` instead * */ @@ -2174,49 +2190,13 @@ export class ZodObject< * inferred type of merged objects. Please * upgrade if you are experiencing issues. */ - // merge(merging: Incoming) { - // return this.extend(merging.shape as Incoming["shape"]); - // } - merge< - Incoming extends AnyZodObject, - Augmentation extends Incoming["shape"], - // NewShape extends extendShape, - // OldOutput = util.flatten>, - // AugOutput = baseObjectOutputType, - // NewOutput = OldOutput & AugOutput, - NewOutput extends { - [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation - ? Augmentation[k]["_output"] - : k extends keyof Output - ? Output[k] - : never; - }, - // OldInput = util.flatten>, - // AugInput = baseObjectInputType, - // NewInput = OldInput & AugInput - NewInput extends { - [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation - ? Augmentation[k]["_input"] - : k extends keyof Input - ? Input[k] - : never; - } - // AKeys extends string | number | symbol = keyof Augmentation, - - // AKeys extends string | number | symbol = keyof Augmentation - >( + merge( merging: Incoming ): ZodObject< - extendShape>, + extendShape, Incoming["_def"]["unknownKeys"], - Incoming["_def"]["catchall"], - NewOutput, - NewInput + Incoming["_def"]["catchall"] > { - // const mergedShape = objectUtil.mergeShapes( - // this._def.shape(), - // merging._def.shape() - // ); const merged: any = new ZodObject({ unknownKeys: merging._def.unknownKeys, catchall: merging._def.catchall, @@ -2226,6 +2206,41 @@ export class ZodObject< }) as any; return merged; } + // merge< + // Incoming extends AnyZodObject, + // Augmentation extends Incoming["shape"], + // NewOutput extends { + // [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation + // ? Augmentation[k]["_output"] + // : k extends keyof Output + // ? Output[k] + // : never; + // }, + // NewInput extends { + // [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation + // ? Augmentation[k]["_input"] + // : k extends keyof Input + // ? Input[k] + // : never; + // } + // >( + // merging: Incoming + // ): ZodObject< + // extendShape>, + // Incoming["_def"]["unknownKeys"], + // Incoming["_def"]["catchall"], + // NewOutput, + // NewInput + // > { + // const merged: any = new ZodObject({ + // unknownKeys: merging._def.unknownKeys, + // catchall: merging._def.catchall, + // shape: () => + // objectUtil.mergeShapes(this._def.shape(), merging._def.shape()), + // typeName: ZodFirstPartyTypeKind.ZodObject, + // }) as any; + // return merged; + // } setKey( key: Key,