From 2562f64a70a28d4b66213def83b6ff3568147a51 Mon Sep 17 00:00:00 2001 From: Ben Tefay Date: Thu, 5 May 2022 20:35:21 +1000 Subject: [PATCH] Increase test coverage for type inference of optional object properties --- deno/lib/__tests__/object.test.ts | 22 ++++++++++++++++++++++ src/__tests__/object.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) 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(),