From fb936ed1c146165cd8911122f7b8072f8d67ca72 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 19 Dec 2024 12:29:40 -0600 Subject: [PATCH] recordWithBrandedKeys should not be partial --- deno/lib/__tests__/record.test.ts | 8 ++++++++ deno/lib/types.ts | 2 +- src/__tests__/record.test.ts | 8 ++++++++ src/types.ts | 2 +- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/deno/lib/__tests__/record.test.ts b/deno/lib/__tests__/record.test.ts index 311f805a7..b750b45c9 100644 --- a/deno/lib/__tests__/record.test.ts +++ b/deno/lib/__tests__/record.test.ts @@ -17,6 +17,9 @@ const recordWithLiteralKeys = z.record( ); type recordWithLiteralKeys = z.infer; +const recordWithBrandedKeys = z.record(z.string().brand("someKey"), z.string()); +type recordWithBrandedKeys = z.infer; + test("type inference", () => { util.assertEqual>(true); @@ -29,6 +32,11 @@ test("type inference", () => { recordWithLiteralKeys, Partial> >(true); + + util.assertEqual< + recordWithBrandedKeys, + Record, string> + >(true); }); test("methods", () => { diff --git a/deno/lib/types.ts b/deno/lib/types.ts index cd09d4b15..4f95a53c0 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -3691,7 +3691,7 @@ export type RecordType = [ ? Record : [symbol] extends [K] ? Record - : [BRAND] extends [K] + : [K] extends [BRAND] ? Record : Partial>; export class ZodRecord< diff --git a/src/__tests__/record.test.ts b/src/__tests__/record.test.ts index 86f60b267..274211025 100644 --- a/src/__tests__/record.test.ts +++ b/src/__tests__/record.test.ts @@ -16,6 +16,9 @@ const recordWithLiteralKeys = z.record( ); type recordWithLiteralKeys = z.infer; +const recordWithBrandedKeys = z.record(z.string().brand("someKey"), z.string()); +type recordWithBrandedKeys = z.infer; + test("type inference", () => { util.assertEqual>(true); @@ -28,6 +31,11 @@ test("type inference", () => { recordWithLiteralKeys, Partial> >(true); + + util.assertEqual< + recordWithBrandedKeys, + Record, string> + >(true); }); test("methods", () => { diff --git a/src/types.ts b/src/types.ts index 98281ff2f..8d1b8f042 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3691,7 +3691,7 @@ export type RecordType = [ ? Record : [symbol] extends [K] ? Record - : [BRAND] extends [K] + : [K] extends [BRAND] ? Record : Partial>; export class ZodRecord<