diff --git a/deno/lib/__tests__/object.test.ts b/deno/lib/__tests__/object.test.ts index fac2b9ffd1..1bd290c14c 100644 --- a/deno/lib/__tests__/object.test.ts +++ b/deno/lib/__tests__/object.test.ts @@ -216,6 +216,28 @@ test("test inferred merged type", async () => { f1; }); +test("inferred merged types with optionals", async () => { + const Merged = z + .object({ a: z.string(), b: z.string().optional() }) + .merge(z.object({ a: z.string().optional(), b: z.string() })); + type Merged = z.infer; + const f1: util.AssertEqual = true; + f1; +}); + +test("inferred unioned types with optionals", async () => { + const Unioned = z.union([ + z.object({ a: z.string(), b: z.string().optional() }), + z.object({ a: z.string().optional(), b: z.string() }), + ]); + type Unioned = z.infer; + const f1: util.AssertEqual< + Unioned, + { a: string; b?: string } | { a?: string; b: string } + > = true; + f1; +}); + test("inferred type for unknown/any keys", () => { const myType = z.object({ anyOptional: z.any().optional(), diff --git a/src/__tests__/object.test.ts b/src/__tests__/object.test.ts index fc01e6397d..3e0ecfc632 100644 --- a/src/__tests__/object.test.ts +++ b/src/__tests__/object.test.ts @@ -215,6 +215,28 @@ test("test inferred merged type", async () => { f1; }); +test("inferred merged types with optionals", async () => { + const Merged = z + .object({ a: z.string(), b: z.string().optional() }) + .merge(z.object({ a: z.string().optional(), b: z.string() })); + type Merged = z.infer; + const f1: util.AssertEqual = true; + f1; +}); + +test("inferred unioned types with optionals", async () => { + const Unioned = z.union([ + z.object({ a: z.string(), b: z.string().optional() }), + z.object({ a: z.string().optional(), b: z.string() }), + ]); + type Unioned = z.infer; + const f1: util.AssertEqual< + Unioned, + { a: string; b?: string } | { a?: string; b: string } + > = true; + f1; +}); + test("inferred type for unknown/any keys", () => { const myType = z.object({ anyOptional: z.any().optional(),