Skip to content

Commit

Permalink
recordWithBrandedKeys should not be partial
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexErrant committed Dec 19, 2024
1 parent f7ad261 commit fb936ed
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
8 changes: 8 additions & 0 deletions deno/lib/__tests__/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const recordWithLiteralKeys = z.record(
);
type recordWithLiteralKeys = z.infer<typeof recordWithLiteralKeys>;

const recordWithBrandedKeys = z.record(z.string().brand("someKey"), z.string());
type recordWithBrandedKeys = z.infer<typeof recordWithBrandedKeys>;

test("type inference", () => {
util.assertEqual<booleanRecord, Record<string, boolean>>(true);

Expand All @@ -29,6 +32,11 @@ test("type inference", () => {
recordWithLiteralKeys,
Partial<Record<"Tuna" | "Salmon", string>>
>(true);

util.assertEqual<
recordWithBrandedKeys,
Record<string & z.BRAND<"someKey">, string>
>(true);
});

test("methods", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3691,7 +3691,7 @@ export type RecordType<K extends string | number | symbol, V> = [
? Record<K, V>
: [symbol] extends [K]
? Record<K, V>
: [BRAND<string | number | symbol>] extends [K]
: [K] extends [BRAND<string | number | symbol>]
? Record<K, V>
: Partial<Record<K, V>>;
export class ZodRecord<
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const recordWithLiteralKeys = z.record(
);
type recordWithLiteralKeys = z.infer<typeof recordWithLiteralKeys>;

const recordWithBrandedKeys = z.record(z.string().brand("someKey"), z.string());
type recordWithBrandedKeys = z.infer<typeof recordWithBrandedKeys>;

test("type inference", () => {
util.assertEqual<booleanRecord, Record<string, boolean>>(true);

Expand All @@ -28,6 +31,11 @@ test("type inference", () => {
recordWithLiteralKeys,
Partial<Record<"Tuna" | "Salmon", string>>
>(true);

util.assertEqual<
recordWithBrandedKeys,
Record<string & z.BRAND<"someKey">, string>
>(true);
});

test("methods", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3691,7 +3691,7 @@ export type RecordType<K extends string | number | symbol, V> = [
? Record<K, V>
: [symbol] extends [K]
? Record<K, V>
: [BRAND<string | number | symbol>] extends [K]
: [K] extends [BRAND<string | number | symbol>]
? Record<K, V>
: Partial<Record<K, V>>;
export class ZodRecord<
Expand Down

0 comments on commit fb936ed

Please sign in to comment.