Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix record type with branded keys #3810

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions deno/lib/__tests__/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ const recordWithLiteralKeys = z.record(
);
type recordWithLiteralKeys = z.infer<typeof recordWithLiteralKeys>;

const StringBrand = z.string().brand("StringBrand");
type StringBrand = z.infer<typeof StringBrand>;
const NumberBrand = z.number().brand("NumberBrand");
type NumberBrand = z.infer<typeof NumberBrand>;
const SymbolBrand = z.symbol().brand("SymbolBrand");
type SymbolBrand = z.infer<typeof SymbolBrand>;

const recordWithBrandedStringKeys = z.record(StringBrand, z.number());
type recordWithBrandedStringKeys = z.infer<typeof recordWithBrandedStringKeys>;
const recordWithBrandedNumberKeys = z.record(NumberBrand, z.number());
type recordWithBrandedNumberKeys = z.infer<typeof recordWithBrandedNumberKeys>;
const recordWithBrandedSymbolKeys = z.record(SymbolBrand, z.number());
type recordWithBrandedSymbolKeys = z.infer<typeof recordWithBrandedSymbolKeys>;

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

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

util.assertEqual<recordWithBrandedStringKeys, Record<StringBrand, number>>(
true
);
util.assertEqual<recordWithBrandedNumberKeys, Record<NumberBrand, number>>(
true
);
util.assertEqual<recordWithBrandedSymbolKeys, Record<SymbolBrand, number>>(
true
);
});

test("methods", () => {
Expand Down
9 changes: 8 additions & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3531,7 +3531,11 @@ export type RecordType<K extends string | number | symbol, V> = [
? Record<K, V>
: [symbol] extends [K]
? Record<K, V>
: [BRAND<string | number | symbol>] extends [K]
: [string & AnyBrand] extends [K]
? Record<K, V>
: [number & AnyBrand] extends [K]
? Record<K, V>
: [symbol & AnyBrand] extends [K]
? Record<K, V>
: Partial<Record<K, V>>;
export class ZodRecord<
Expand Down Expand Up @@ -4904,6 +4908,9 @@ export const BRAND: unique symbol = Symbol("zod_brand");
export type BRAND<T extends string | number | symbol> = {
[BRAND]: { [k in T]: true };
};
export type AnyBrand = {
[BRAND]: any;
};

export class ZodBranded<
T extends ZodTypeAny,
Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ const recordWithLiteralKeys = z.record(
);
type recordWithLiteralKeys = z.infer<typeof recordWithLiteralKeys>;

const StringBrand = z.string().brand("StringBrand");
type StringBrand = z.infer<typeof StringBrand>;
const NumberBrand = z.number().brand("NumberBrand");
type NumberBrand = z.infer<typeof NumberBrand>;
const SymbolBrand = z.symbol().brand("SymbolBrand");
type SymbolBrand = z.infer<typeof SymbolBrand>;

const recordWithBrandedStringKeys = z.record(StringBrand, z.number());
type recordWithBrandedStringKeys = z.infer<typeof recordWithBrandedStringKeys>;
const recordWithBrandedNumberKeys = z.record(NumberBrand, z.number());
type recordWithBrandedNumberKeys = z.infer<typeof recordWithBrandedNumberKeys>;
const recordWithBrandedSymbolKeys = z.record(SymbolBrand, z.number());
type recordWithBrandedSymbolKeys = z.infer<typeof recordWithBrandedSymbolKeys>;

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

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

util.assertEqual<recordWithBrandedStringKeys, Record<StringBrand, number>>(
true
);
util.assertEqual<recordWithBrandedNumberKeys, Record<NumberBrand, number>>(
true
);
util.assertEqual<recordWithBrandedSymbolKeys, Record<SymbolBrand, number>>(
true
);
});

test("methods", () => {
Expand Down
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3531,7 +3531,11 @@ export type RecordType<K extends string | number | symbol, V> = [
? Record<K, V>
: [symbol] extends [K]
? Record<K, V>
: [BRAND<string | number | symbol>] extends [K]
: [string & AnyBrand] extends [K]
? Record<K, V>
: [number & AnyBrand] extends [K]
? Record<K, V>
: [symbol & AnyBrand] extends [K]
? Record<K, V>
: Partial<Record<K, V>>;
export class ZodRecord<
Expand Down Expand Up @@ -4904,6 +4908,9 @@ export const BRAND: unique symbol = Symbol("zod_brand");
export type BRAND<T extends string | number | symbol> = {
[BRAND]: { [k in T]: true };
};
export type AnyBrand = {
[BRAND]: any;
};

export class ZodBranded<
T extends ZodTypeAny,
Expand Down