Skip to content

Commit aff9561

Browse files
committed
Fix branded types in record
1 parent 4a3baf7 commit aff9561

File tree

3 files changed

+29
-39
lines changed

3 files changed

+29
-39
lines changed

packages/zod/src/v4/classic/tests/brand.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@ test("$branded", () => {
5555

5656
expectTypeOf<typeof a>().toEqualTypeOf<z.core.$ZodBranded<z.ZodString, "a">>();
5757
});
58+
59+
test("branded record", () => {
60+
const recordWithBrandedNumberKeys = z.record(z.string().brand("SomeBrand"), z.number());
61+
type recordWithBrandedNumberKeys = z.infer<typeof recordWithBrandedNumberKeys>;
62+
expectTypeOf<recordWithBrandedNumberKeys>().toEqualTypeOf<
63+
Record<string & z.core.$brand<"SomeBrand">, number | undefined>
64+
>();
65+
});

packages/zod/src/v4/core/schemas.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,31 +2324,31 @@ export interface $ZodRecordDef extends $ZodTypeDef {
23242324
valueType: $ZodType;
23252325
}
23262326

2327-
type $InferZodRecordOutput<
2327+
export type $InferZodRecordOutput<
23282328
Key extends $ZodRecordKey = $ZodRecordKey,
23292329
Value extends $ZodType = $ZodType,
23302330
> = undefined extends Key["_zod"]["values"]
2331-
? string extends Key["_zod"]["output"]
2332-
? Record<Key["_zod"]["output"], core.output<Value>>
2333-
: number extends Key["_zod"]["output"]
2334-
? Record<Key["_zod"]["output"], core.output<Value>>
2335-
: symbol extends Key["_zod"]["output"]
2336-
? Record<Key["_zod"]["output"], core.output<Value>>
2337-
: Partial<Record<Key["_zod"]["output"], core.output<Value>>>
2338-
: Record<Key["_zod"]["output"], core.output<Value>>;
2339-
2340-
type $InferZodRecordInput<
2331+
? string extends core.output<Key>
2332+
? Record<core.output<Key>, core.output<Value>>
2333+
: number extends core.output<Key>
2334+
? Record<core.output<Key>, core.output<Value>>
2335+
: symbol extends core.output<Key>
2336+
? Record<core.output<Key>, core.output<Value>>
2337+
: Partial<Record<core.output<Key>, core.output<Value>>>
2338+
: Record<core.output<Key>, core.output<Value>>;
2339+
2340+
export type $InferZodRecordInput<
23412341
Key extends $ZodRecordKey = $ZodRecordKey,
23422342
Value extends $ZodType = $ZodType,
23432343
> = undefined extends Key["_zod"]["values"]
2344-
? string extends Key["_zod"]["input"]
2345-
? Record<Key["_zod"]["input"], Value["_zod"]["input"]>
2346-
: number extends Key["_zod"]["input"]
2347-
? Record<Key["_zod"]["input"], Value["_zod"]["input"]>
2348-
: symbol extends Key["_zod"]["input"]
2349-
? Record<Key["_zod"]["input"], Value["_zod"]["input"]>
2350-
: Partial<Record<Key["_zod"]["input"], Value["_zod"]["input"]>>
2351-
: Record<Key["_zod"]["input"], Value["_zod"]["input"]>;
2344+
? string extends core.input<Key>
2345+
? Record<core.input<Key>, core.input<Value>>
2346+
: number extends core.input<Key>
2347+
? Record<core.input<Key>, core.input<Value>>
2348+
: symbol extends core.input<Key>
2349+
? Record<core.input<Key>, core.input<Value>>
2350+
: Partial<Record<core.input<Key>, core.input<Value>>>
2351+
: Record<core.input<Key>, core.input<Value>>;
23522352

23532353
export interface $ZodRecordInternals<Key extends $ZodRecordKey = $ZodRecordKey, Value extends $ZodType = $ZodType>
23542354
extends $ZodTypeInternals<$InferZodRecordOutput<Key, Value>, $InferZodRecordInput<Key, Value>> {

play.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
11
import { z } from "zod/v4";
22

33
z;
4-
import { discriminatedUnion, literal, object, pipe, string, transform } from "zod/v4-mini";
5-
6-
const schemaWithPipe = discriminatedUnion("type", [
7-
object({
8-
type: pipe(
9-
transform((v) => v),
10-
literal("a")
11-
),
12-
a: string(),
13-
}),
14-
object({
15-
type: pipe(
16-
transform((v) => v),
17-
literal("b")
18-
),
19-
b: string(),
20-
}),
21-
]);
22-
23-
schemaWithPipe.safeParse({ type: "a", a: "abc" });
4+
const recordWithBrandedNumberKeys = z.record(z.number().brand("SomeBrand"), z.number());
5+
type recordWithBrandedNumberKeys = z.infer<typeof recordWithBrandedNumberKeys>;

0 commit comments

Comments
 (0)