From a5830c6d3fe28f0d5435cc9ff22846be7f5977c1 Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Mon, 6 Mar 2023 13:18:39 -0800 Subject: [PATCH] Reverted #1564 --- deno/lib/__tests__/partials.test.ts | 29 ----------------------------- deno/lib/types.ts | 18 ++++++++---------- playground.ts | 8 +------- src/__tests__/partials.test.ts | 29 ----------------------------- src/types.ts | 18 ++++++++---------- 5 files changed, 17 insertions(+), 85 deletions(-) diff --git a/deno/lib/__tests__/partials.test.ts b/deno/lib/__tests__/partials.test.ts index 06fb29ba8..1825753a9 100644 --- a/deno/lib/__tests__/partials.test.ts +++ b/deno/lib/__tests__/partials.test.ts @@ -185,20 +185,6 @@ test("required with mask", () => { expect(requiredObject.shape.country).toBeInstanceOf(z.ZodOptional); }); -test("required with mask containing a nonexistent key", () => { - const object = z.object({ - name: z.string(), - age: z.number().optional(), - field: z.string().optional().default("asdf"), - country: z.string().optional(), - }); - object.required({ - age: true, - // @ts-expect-error should not accept unexpected keys. - doesntExist: true, - }); -}); - test("required with mask -- ignore falsy values", () => { const object = z.object({ name: z.string(), @@ -256,21 +242,6 @@ test("partial with mask -- ignore falsy values", async () => { await masked.parseAsync({ country: "US" }); }); -test("partial with mask containing a nonexistent key", () => { - const object = z.object({ - name: z.string(), - age: z.number().optional(), - field: z.string().optional().default("asdf"), - country: z.string().optional(), - }); - - object.partial({ - age: true, - // @ts-expect-error should not accept unexpected keys. - doesntExist: true, - }); -}); - test("deeppartial array", () => { const schema = z.object({ array: z.string().array().min(42) }).deepPartial(); diff --git a/deno/lib/types.ts b/deno/lib/types.ts index d9aecc9c5..7b3ee92cb 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -2182,8 +2182,6 @@ export type SomeZodObject = ZodObject< ZodTypeAny >; -export type objectKeyMask = { [k in keyof Obj]?: true }; - export type noUnrecognized = { [k in keyof Obj]: k extends keyof Shape ? Obj[k] : never; }; @@ -2550,8 +2548,8 @@ export class ZodObject< }) as any; } - pick>( - mask: noUnrecognized + pick( + mask: Mask ): ZodObject>, UnknownKeys, Catchall> { const shape: any = {}; @@ -2567,8 +2565,8 @@ export class ZodObject< }) as any; } - omit>( - mask: noUnrecognized> + omit( + mask: Mask ): ZodObject, UnknownKeys, Catchall> { const shape: any = {}; @@ -2596,8 +2594,8 @@ export class ZodObject< UnknownKeys, Catchall >; - partial>( - mask: noUnrecognized> + partial( + mask: Mask ): ZodObject< objectUtil.noNever<{ [k in keyof T]: k extends keyof Mask ? ZodOptional : T[k]; @@ -2629,8 +2627,8 @@ export class ZodObject< UnknownKeys, Catchall >; - required>( - mask: noUnrecognized> + required( + mask: Mask ): ZodObject< objectUtil.noNever<{ [k in keyof T]: k extends keyof Mask ? deoptional : T[k]; diff --git a/playground.ts b/playground.ts index 24fb4f8f8..d17b18eb4 100644 --- a/playground.ts +++ b/playground.ts @@ -1,8 +1,2 @@ import { z } from "./src"; - -function foo(schema: Schema) { - return z.object({ bar: schema }).transform((x) => x.bar); - // ^^^ - // Property 'bar' does not exist on type - // '{ [k in keyof baseObjectOutputType<{ bar: Schema; }>]: baseObjectOutputType<{ bar: Schema; }>[k]; }'. -} +z; diff --git a/src/__tests__/partials.test.ts b/src/__tests__/partials.test.ts index d716b4471..3186c9f97 100644 --- a/src/__tests__/partials.test.ts +++ b/src/__tests__/partials.test.ts @@ -184,20 +184,6 @@ test("required with mask", () => { expect(requiredObject.shape.country).toBeInstanceOf(z.ZodOptional); }); -test("required with mask containing a nonexistent key", () => { - const object = z.object({ - name: z.string(), - age: z.number().optional(), - field: z.string().optional().default("asdf"), - country: z.string().optional(), - }); - object.required({ - age: true, - // @ts-expect-error should not accept unexpected keys. - doesntExist: true, - }); -}); - test("required with mask -- ignore falsy values", () => { const object = z.object({ name: z.string(), @@ -255,21 +241,6 @@ test("partial with mask -- ignore falsy values", async () => { await masked.parseAsync({ country: "US" }); }); -test("partial with mask containing a nonexistent key", () => { - const object = z.object({ - name: z.string(), - age: z.number().optional(), - field: z.string().optional().default("asdf"), - country: z.string().optional(), - }); - - object.partial({ - age: true, - // @ts-expect-error should not accept unexpected keys. - doesntExist: true, - }); -}); - test("deeppartial array", () => { const schema = z.object({ array: z.string().array().min(42) }).deepPartial(); diff --git a/src/types.ts b/src/types.ts index 718d082e1..81c2dcde5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2182,8 +2182,6 @@ export type SomeZodObject = ZodObject< ZodTypeAny >; -export type objectKeyMask = { [k in keyof Obj]?: true }; - export type noUnrecognized = { [k in keyof Obj]: k extends keyof Shape ? Obj[k] : never; }; @@ -2550,8 +2548,8 @@ export class ZodObject< }) as any; } - pick>( - mask: noUnrecognized + pick( + mask: Mask ): ZodObject>, UnknownKeys, Catchall> { const shape: any = {}; @@ -2567,8 +2565,8 @@ export class ZodObject< }) as any; } - omit>( - mask: noUnrecognized> + omit( + mask: Mask ): ZodObject, UnknownKeys, Catchall> { const shape: any = {}; @@ -2596,8 +2594,8 @@ export class ZodObject< UnknownKeys, Catchall >; - partial>( - mask: noUnrecognized> + partial( + mask: Mask ): ZodObject< objectUtil.noNever<{ [k in keyof T]: k extends keyof Mask ? ZodOptional : T[k]; @@ -2629,8 +2627,8 @@ export class ZodObject< UnknownKeys, Catchall >; - required>( - mask: noUnrecognized> + required( + mask: Mask ): ZodObject< objectUtil.noNever<{ [k in keyof T]: k extends keyof Mask ? deoptional : T[k];