Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Zod · Module

z.any

z.any() defines a schema that accepts values of any type and infers any in TypeScript. As an object property, it requires the key unless combined with .optional().

How it works

z.any() creates a schema whose inferred TypeScript type is any and whose validation accepts arbitrary input values. When used as an object property, the property remains required; call .optional() when the property itself may be absent.

Syntax

z.any()

Returns

A Zod schema accepting any value and inferring the TypeScript type any.

Key points

  • z.any() accepts arbitrary input values rather than restricting them to a specific primitive, object, or collection type.
  • The schema's inferred TypeScript type is any, so downstream static checking does not narrow the accepted value.
  • z.any() differs from the related z.unknown() schema: the former infers any, while the latter infers unknown.
  • A plain z.any() property is required when nested in an object; parsing an object that omits the key fails.
  • The required key can contain undefined; presence of the key, rather than its value's type, satisfies the property.
  • Appending .optional() changes the object-property behavior so the key may be omitted.

Common mistakes

  • Using z.any() does not make an object key optional. An object must contain the key, even when its value is undefined; use .optional() to allow omission.

Example: Accept arbitrary values

import { z } from "zod";

const Payload = z.object({
  value: z.any(),
  optionalValue: z.any().optional(),
});

Payload.parse({ value: undefined });
Payload.parse({ value: { source: "external" } });
Payload.parse({ value: 42, optionalValue: undefined });

z.any() accepts values of any runtime type. In an object schema, the property is still required unless .optional() is added.

When to use it

Accept arbitrary input

Validating untyped or heterogeneous external data when no runtime value restriction is appropriate.

Required unrestricted property

Representing an object field that may contain any value, including undefined, while still requiring the field to exist.

Optional unrestricted property

Allowing an object field to be absent as well as unrestricted when present.

Interview questions

QWhat does z.any() validate and infer?

z.any() is Zod's schema for arbitrary values, and its inferred TypeScript type is any. It accepts values without restricting their runtime type.

Follow-up: How do you allow an object property using z.any() to be omitted? Use z.any().optional(), because the base schema still requires the property key.

QDoes z.any() make an object property optional?

In an object schema, z.any() makes the property key required. The value may be undefined, but the key itself must be present.

Follow-up: What input distinguishes the two forms? { a: undefined } satisfies z.object({ a: z.any() }), while {} does not; adding .optional() makes {} valid.

QHow would you model an optional arbitrary-valued property?

Use z.any().optional() when both arbitrary values and an absent property are allowed. The documentation contrasts this with plain z.any(), which requires the key.

Follow-up: What TypeScript shape does this correspond to? The optional form corresponds to a property that may be omitted, unlike the required { a: any } shape.

  • z.unknown()
  • z.object()
  • z.optional()

Source: Defining schemas - Zod. Checked 2026-10-15.