Skip to content

Commit 7afe790

Browse files
committed
Make $ZodLiteralDef generic
1 parent fb00618 commit 7afe790

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ export function array<T extends core.SomeType>(element: T, params?: string | cor
10151015
}
10161016

10171017
// .keyof
1018-
export function keyof<T extends ZodObject>(schema: T): ZodLiteral<keyof T["_zod"]["output"]> {
1018+
export function keyof<T extends ZodObject>(schema: T): ZodLiteral<Exclude<keyof T["_zod"]["output"], symbol>> {
10191019
const shape = schema._zod.def.shape;
10201020
return literal(Object.keys(shape)) as any;
10211021
}
@@ -1487,7 +1487,7 @@ export function nativeEnum<T extends util.EnumLike>(entries: T, params?: string
14871487
}
14881488

14891489
// ZodLiteral
1490-
export interface ZodLiteral<T extends util.Primitive = util.Primitive>
1490+
export interface ZodLiteral<T extends util.Literal = util.Literal>
14911491
extends _ZodType<core.$ZodLiteralInternals<T>>,
14921492
core.$ZodLiteral<T> {
14931493
values: Set<T>;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,19 +2687,19 @@ export const $ZodEnum: core.$constructor<$ZodEnum> = /*@__PURE__*/ core.$constru
26872687
////////////////////////////////////////
26882688
////////////////////////////////////////
26892689

2690-
export interface $ZodLiteralDef extends $ZodTypeDef {
2690+
export interface $ZodLiteralDef<T extends util.Literal> extends $ZodTypeDef {
26912691
type: "literal";
2692-
values: util.LiteralArray;
2692+
values: T[];
26932693
}
26942694

2695-
export interface $ZodLiteralInternals<T extends util.Primitive = util.Primitive> extends $ZodTypeInternals<T, T> {
2696-
def: $ZodLiteralDef;
2695+
export interface $ZodLiteralInternals<T extends util.Literal = util.Literal> extends $ZodTypeInternals<T, T> {
2696+
def: $ZodLiteralDef<T>;
26972697
values: Set<T>;
26982698
pattern: RegExp;
26992699
isst: errors.$ZodIssueInvalidValue;
27002700
}
27012701

2702-
export interface $ZodLiteral<T extends util.Primitive = util.Primitive> extends $ZodType {
2702+
export interface $ZodLiteral<T extends util.Literal = util.Literal> extends $ZodType {
27032703
_zod: $ZodLiteralInternals<T>;
27042704
}
27052705

@@ -2708,7 +2708,7 @@ export const $ZodLiteral: core.$constructor<$ZodLiteral> = /*@__PURE__*/ core.$c
27082708
(inst, def) => {
27092709
$ZodType.init(inst, def);
27102710

2711-
inst._zod.values = new Set<util.Primitive>(def.values);
2711+
inst._zod.values = new Set<util.Literal>(def.values);
27122712
inst._zod.pattern = new RegExp(
27132713
`^(${def.values
27142714

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ export function array<T extends SomeType>(element: SomeType, params?: any): ZodM
686686
}
687687

688688
// .keyof
689-
export function keyof<T extends ZodMiniObject>(schema: T): ZodMiniLiteral<keyof T["shape"]> {
689+
export function keyof<T extends ZodMiniObject>(schema: T): ZodMiniLiteral<Exclude<keyof T["shape"], symbol>> {
690690
const shape = schema._zod.def.shape;
691691
return literal(Object.keys(shape)) as any;
692692
}
@@ -1094,7 +1094,7 @@ export function nativeEnum<T extends util.EnumLike>(entries: T, params?: string
10941094
}
10951095

10961096
// ZodMiniLiteral
1097-
export interface ZodMiniLiteral<T extends util.Primitive = util.Primitive>
1097+
export interface ZodMiniLiteral<T extends util.Literal = util.Literal>
10981098
extends _ZodMiniType<core.$ZodLiteralInternals<T>> {
10991099
// _zod: core.$ZodLiteralInternals<T>;
11001100
}

packages/zod/src/v4/mini/tests/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ test("def typing", () => {
852852
z.map(z.string(), z.number()).def.type satisfies "map";
853853
z.set(z.string()).def.type satisfies "set";
854854
z.literal("example").def.type satisfies "literal";
855+
expectTypeOf(z.literal("example").def.values).toEqualTypeOf<"example"[]>();
855856
z.enum(["a", "b", "c"]).def.type satisfies "enum";
856857
z.promise(z.string()).def.type satisfies "promise";
857858
z.lazy(() => z.string()).def.type satisfies "lazy";

play.ts

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

33
z.string().regex(/asdf/);
4+
5+
z.literal;

0 commit comments

Comments
 (0)