Skip to content

Commit

Permalink
Increase test coverage for type inference of optional object properties
Browse files Browse the repository at this point in the history
  • Loading branch information
bentefay committed May 5, 2022
1 parent 4cf7e7c commit 2562f64
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions deno/lib/__tests__/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof Merged>;
const f1: util.AssertEqual<Merged, { a?: string; b: string }> = 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<typeof Unioned>;
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(),
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof Merged>;
const f1: util.AssertEqual<Merged, { a?: string; b: string }> = 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<typeof Unioned>;
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(),
Expand Down

0 comments on commit 2562f64

Please sign in to comment.