Skip to main content
next-safe-env exports a set of TypeScript types you can use to annotate functions, derive types from your schema, or build wrapper utilities. All types are re-exported from the package root, so you can import them directly from next-safe-env. None of these types have a runtime representation - they exist solely in the TypeScript type system.

ServerOnly<T>

ServerOnly<T> brands a type T with a unique symbol that marks it as originating from the server schema. Every value in the returned env object that comes from a server key carries this brand, making its origin visible in IDE tooltips.
Because TypeScript uses structural typing, ServerOnly<T> is assignment-compatible with T. It does not produce a compile error by itself. To get a hard compile error when client components import server vars, add import 'server-only' at the top of your env.ts file.

ClientEnv<TEnv>

ClientEnv<TEnv> strips every ServerOnly-branded key from an env type, leaving only the client-safe keys. Use it to constrain functions or components that must not access server variables.

InferEnv<TServer, TClient>

InferEnv<TServer, TClient> is the full return type of createEnv(). Server keys are wrapped in ServerOnly<T>; client keys are their plain inferred types. The whole object is Readonly.
You rarely need to reference InferEnv directly - the return type of createEnv() is inferred automatically. Use it when you need to pass the env type as a generic argument elsewhere.

InferSchema<T>

InferSchema<T> maps a Schema (a Record<string, FieldValidator<unknown>>) to the object type its validators produce. It reads the phantom _type property from each FieldValidator to extract the output type.

InferInput<T>

InferInput<T> infers the output type from either a native Schema or a ZodObjectLike. For Zod schemas it reads the _output phantom property; for native schemas it delegates to InferSchema<T>.
This type is used internally by InferEnv to handle both native and Zod schemas uniformly.

EnvConfig<TServer, TClient>

EnvConfig<TServer, TClient> is the shape of the config object passed to createEnv(). Refer to the createEnv() reference for a full description of each field.

ValidationFailure

ValidationFailure describes a single failed field produced during validation. An array of these is attached to EnvValidationError and passed to onValidationError.
string
required
The environment variable name that failed, e.g. "DATABASE_URL".
string
required
A human-readable description of what was expected, e.g. "valid URL" or "length >= 32".
string
required
The actual value (or "undefined") that was received.
string
required
The full error message combining expected and received, e.g. "Expected valid URL. Got: \"postgres-localhost\"".

ValidationStats

ValidationStats provides a summary count of how many variables were checked and how many failed, split by server and client. It is attached to EnvValidationError.
number
required
Total number of keys in the server schema.
number
required
Total number of keys in the client schema.
number
required
Number of server keys that failed validation.
number
required
Number of client keys that failed validation.

FieldValidator<T>

FieldValidator<T> is the interface every validator class implements. The generic T is the TypeScript output type - string, number, boolean, or their | undefined variants. The _type property is a phantom: it never exists at runtime and is only used by InferSchema<T> to extract T.
boolean
required
true when .optional() has been called on the validator.
T | undefined
required
The fallback value set by .default(v), or undefined if no default was configured.
(rawValue: unknown, fieldName: string) => T
required
Parses and validates rawValue. Throws a FieldError if the value is invalid. Called once per field during the validation pass inside createEnv().

Adapter

Adapter is the interface that all built-in adapters (nextjs, node, edge, vite) implement. You can also implement this interface to build a custom adapter.
string
required
Identifier for the adapter, e.g. "nextjs" or "node".
function
required
Called before any field is validated. Receives the server schema, client schema, and raw env map. Can throw to block startup (e.g., when a NEXT_PUBLIC_ prefix is missing). Returns the raw env map - possibly filtered - that validation runs against.
function
required
Called after all fields pass validation. Receives the fully-validated env object and returns it, optionally with keys stripped (e.g., the edge adapter removes all non-NEXT_PUBLIC_ keys unconditionally).

Zod interop types

These types are exported for advanced Zod interop use cases. They are duck-typed interfaces - no zod package import is required.

ZodTypeLike<T>

A duck-typed interface matching zod.ZodType. createEnv() detects Zod schemas at runtime using this structural shape.

ZodObjectLike<T>

A duck-typed interface matching zod.ZodObject. Required for the createEnv({ server: z.object({ ... }) }) syntax.

ZodErrorLike

A duck-typed interface matching the error shape exposed by zod.ZodError.