zod 4.1.0-canary.20250804T184136 → 4.1.0-canary.20250806T000520

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "4.1.0-canary.20250804T184136",
3
+ "version": "4.1.0-canary.20250806T000520",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "Colin McDonnell <zod@colinhacks.com>",
@@ -1038,9 +1038,9 @@ export function array<T extends core.SomeType>(element: T, params?: string | cor
1038
1038
  }
1039
1039
 
1040
1040
  // .keyof
1041
- export function keyof<T extends ZodObject>(schema: T): ZodLiteral<Exclude<keyof T["_zod"]["output"], symbol>> {
1041
+ export function keyof<T extends ZodObject>(schema: T): ZodEnum<util.KeysEnum<T["_zod"]["output"]>> {
1042
1042
  const shape = schema._zod.def.shape;
1043
- return literal(Object.keys(shape)) as any;
1043
+ return _enum(Object.keys(shape)) as any;
1044
1044
  }
1045
1045
 
1046
1046
  // ZodObject
@@ -261,6 +261,21 @@ test("inferred enum type", async () => {
261
261
  expectTypeOf<Enum>().toEqualTypeOf<"a" | "b">();
262
262
  });
263
263
 
264
+ test("z.keyof returns enum", () => {
265
+ const User = z.object({ name: z.string(), age: z.number() });
266
+ const keysSchema = z.keyof(User);
267
+ expect(keysSchema.enum).toEqual({
268
+ name: "name",
269
+ age: "age",
270
+ });
271
+ expect(keysSchema._zod.def.entries).toEqual({
272
+ name: "name",
273
+ age: "age",
274
+ });
275
+ type Keys = z.infer<typeof keysSchema>;
276
+ expectTypeOf<Keys>().toEqualTypeOf<"name" | "age">();
277
+ });
278
+
264
279
  test("inferred partial object type with optional properties", async () => {
265
280
  const Partial = z.object({ a: z.string(), b: z.string().optional() }).partial();
266
281
  type Partial = z.infer<typeof Partial>;
@@ -84,8 +84,8 @@ export class $ZodAsyncError extends Error {
84
84
  // export type output<T extends schemas.$ZodType> = T["_zod"]["output"];
85
85
  // export type input<T extends schemas.$ZodType> = T["_zod"]["input"];
86
86
  // export type output<T extends schemas.$ZodType> = T["_zod"]["output"];
87
- export type input<T> = T extends { _zod: { input: any } } ? Required<T["_zod"]>["input"] : unknown;
88
- export type output<T> = T extends { _zod: { output: any } } ? Required<T["_zod"]>["output"] : unknown;
87
+ export type input<T> = T extends { _zod: { input: any } } ? T["_zod"]["input"] : unknown;
88
+ export type output<T> = T extends { _zod: { output: any } } ? T["_zod"]["output"] : unknown;
89
89
 
90
90
  // Mk2
91
91
  // export type input<T> = T extends { _zod: { "~input": any } }
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 0,
4
- patch: 14 as number,
4
+ patch: 15 as number,
5
5
  } as const;
@@ -712,9 +712,9 @@ export function array<T extends SomeType>(element: SomeType, params?: any): ZodM
712
712
  }
713
713
 
714
714
  // .keyof
715
- export function keyof<T extends ZodMiniObject>(schema: T): ZodMiniLiteral<Exclude<keyof T["shape"], symbol>> {
715
+ export function keyof<T extends ZodMiniObject>(schema: T): ZodMiniEnum<util.KeysEnum<T["shape"]>> {
716
716
  const shape = schema._zod.def.shape;
717
- return literal(Object.keys(shape)) as any;
717
+ return _enum(Object.keys(shape)) as any;
718
718
  }
719
719
 
720
720
  // ZodMiniObject
@@ -89,6 +89,12 @@ test("z.keyof", () => {
89
89
  type UserKeys = z.infer<typeof userKeysSchema>;
90
90
  expectTypeOf<UserKeys>().toEqualTypeOf<"name" | "age" | "email">();
91
91
  expect(userKeysSchema).toBeDefined();
92
+ expect(userKeysSchema._zod.def.type).toBe("enum");
93
+ expect(userKeysSchema._zod.def.entries).toEqual({
94
+ name: "name",
95
+ age: "age",
96
+ email: "email",
97
+ });
92
98
  expect(z.safeParse(userKeysSchema, "name").success).toBe(true);
93
99
  expect(z.safeParse(userKeysSchema, "age").success).toBe(true);
94
100
  expect(z.safeParse(userKeysSchema, "email").success).toBe(true);
@@ -590,7 +590,7 @@ function array(element, params) {
590
590
  // .keyof
591
591
  function keyof(schema) {
592
592
  const shape = schema._zod.def.shape;
593
- return literal(Object.keys(shape));
593
+ return _enum(Object.keys(shape));
594
594
  }
595
595
  exports.ZodObject = core.$constructor("ZodObject", (inst, def) => {
596
596
  core.$ZodObject.init(inst, def);
@@ -395,7 +395,7 @@ export interface ZodArray<T extends core.SomeType = core.$ZodType> extends _ZodT
395
395
  }
396
396
  export declare const ZodArray: core.$constructor<ZodArray>;
397
397
  export declare function array<T extends core.SomeType>(element: T, params?: string | core.$ZodArrayParams): ZodArray<T>;
398
- export declare function keyof<T extends ZodObject>(schema: T): ZodLiteral<Exclude<keyof T["_zod"]["output"], symbol>>;
398
+ export declare function keyof<T extends ZodObject>(schema: T): ZodEnum<util.KeysEnum<T["_zod"]["output"]>>;
399
399
  export interface ZodObject<
400
400
  /** @ts-ignore Cast variance */
401
401
  out Shape extends core.$ZodShape = core.$ZodLooseShape, out Config extends core.$ZodObjectConfig = core.$strip> extends _ZodType<core.$ZodObjectInternals<Shape, Config>>, core.$ZodObject<Shape, Config> {
@@ -395,7 +395,7 @@ export interface ZodArray<T extends core.SomeType = core.$ZodType> extends _ZodT
395
395
  }
396
396
  export declare const ZodArray: core.$constructor<ZodArray>;
397
397
  export declare function array<T extends core.SomeType>(element: T, params?: string | core.$ZodArrayParams): ZodArray<T>;
398
- export declare function keyof<T extends ZodObject>(schema: T): ZodLiteral<Exclude<keyof T["_zod"]["output"], symbol>>;
398
+ export declare function keyof<T extends ZodObject>(schema: T): ZodEnum<util.KeysEnum<T["_zod"]["output"]>>;
399
399
  export interface ZodObject<
400
400
  /** @ts-ignore Cast variance */
401
401
  out Shape extends core.$ZodShape = core.$ZodLooseShape, out Config extends core.$ZodObjectConfig = core.$strip> extends _ZodType<core.$ZodObjectInternals<Shape, Config>>, core.$ZodObject<Shape, Config> {
@@ -484,7 +484,7 @@ export function array(element, params) {
484
484
  // .keyof
485
485
  export function keyof(schema) {
486
486
  const shape = schema._zod.def.shape;
487
- return literal(Object.keys(shape));
487
+ return _enum(Object.keys(shape));
488
488
  }
489
489
  export const ZodObject = /*@__PURE__*/ core.$constructor("ZodObject", (inst, def) => {
490
490
  core.$ZodObject.init(inst, def);
@@ -30,12 +30,12 @@ export type input<T> = T extends {
30
30
  _zod: {
31
31
  input: any;
32
32
  };
33
- } ? Required<T["_zod"]>["input"] : unknown;
33
+ } ? T["_zod"]["input"] : unknown;
34
34
  export type output<T> = T extends {
35
35
  _zod: {
36
36
  output: any;
37
37
  };
38
- } ? Required<T["_zod"]>["output"] : unknown;
38
+ } ? T["_zod"]["output"] : unknown;
39
39
  export type { output as infer };
40
40
  export interface $ZodConfig {
41
41
  /** Custom error map. Overrides `config().localeError`. */
package/v4/core/core.d.ts CHANGED
@@ -30,12 +30,12 @@ export type input<T> = T extends {
30
30
  _zod: {
31
31
  input: any;
32
32
  };
33
- } ? Required<T["_zod"]>["input"] : unknown;
33
+ } ? T["_zod"]["input"] : unknown;
34
34
  export type output<T> = T extends {
35
35
  _zod: {
36
36
  output: any;
37
37
  };
38
- } ? Required<T["_zod"]>["output"] : unknown;
38
+ } ? T["_zod"]["output"] : unknown;
39
39
  export type { output as infer };
40
40
  export interface $ZodConfig {
41
41
  /** Custom error map. Overrides `config().localeError`. */
@@ -4,5 +4,5 @@ exports.version = void 0;
4
4
  exports.version = {
5
5
  major: 4,
6
6
  minor: 0,
7
- patch: 14,
7
+ patch: 15,
8
8
  };
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 0,
4
- patch: 14,
4
+ patch: 15,
5
5
  };
@@ -435,7 +435,7 @@ function array(element, params) {
435
435
  // .keyof
436
436
  function keyof(schema) {
437
437
  const shape = schema._zod.def.shape;
438
- return literal(Object.keys(shape));
438
+ return _enum(Object.keys(shape));
439
439
  }
440
440
  exports.ZodMiniObject = core.$constructor("ZodMiniObject", (inst, def) => {
441
441
  core.$ZodObject.init(inst, def);
@@ -179,7 +179,7 @@ export interface ZodMiniArray<T extends SomeType = core.$ZodType> extends _ZodMi
179
179
  }
180
180
  export declare const ZodMiniArray: core.$constructor<ZodMiniArray>;
181
181
  export declare function array<T extends SomeType>(element: T, params?: string | core.$ZodArrayParams): ZodMiniArray<T>;
182
- export declare function keyof<T extends ZodMiniObject>(schema: T): ZodMiniLiteral<Exclude<keyof T["shape"], symbol>>;
182
+ export declare function keyof<T extends ZodMiniObject>(schema: T): ZodMiniEnum<util.KeysEnum<T["shape"]>>;
183
183
  export interface ZodMiniObject<
184
184
  /** @ts-ignore Cast variance */
185
185
  out Shape extends core.$ZodShape = core.$ZodShape, out Config extends core.$ZodObjectConfig = core.$strip> extends ZodMiniType<any, any, core.$ZodObjectInternals<Shape, Config>>, core.$ZodObject<Shape, Config> {
@@ -179,7 +179,7 @@ export interface ZodMiniArray<T extends SomeType = core.$ZodType> extends _ZodMi
179
179
  }
180
180
  export declare const ZodMiniArray: core.$constructor<ZodMiniArray>;
181
181
  export declare function array<T extends SomeType>(element: T, params?: string | core.$ZodArrayParams): ZodMiniArray<T>;
182
- export declare function keyof<T extends ZodMiniObject>(schema: T): ZodMiniLiteral<Exclude<keyof T["shape"], symbol>>;
182
+ export declare function keyof<T extends ZodMiniObject>(schema: T): ZodMiniEnum<util.KeysEnum<T["shape"]>>;
183
183
  export interface ZodMiniObject<
184
184
  /** @ts-ignore Cast variance */
185
185
  out Shape extends core.$ZodShape = core.$ZodShape, out Config extends core.$ZodObjectConfig = core.$strip> extends ZodMiniType<any, any, core.$ZodObjectInternals<Shape, Config>>, core.$ZodObject<Shape, Config> {
@@ -323,7 +323,7 @@ export function array(element, params) {
323
323
  // .keyof
324
324
  export function keyof(schema) {
325
325
  const shape = schema._zod.def.shape;
326
- return literal(Object.keys(shape));
326
+ return _enum(Object.keys(shape));
327
327
  }
328
328
  export const ZodMiniObject = /*@__PURE__*/ core.$constructor("ZodMiniObject", (inst, def) => {
329
329
  core.$ZodObject.init(inst, def);