Skip to main content
zerotal

Documentation


Documentation / zerotal / env / Def

Class: Def<T>

Defined in: packages/core/src/env/Def.ts:67

A typed field definition produced by the t builder. Carry it directly in EnvSchema.define() — the generic T is inferred automatically.

Example

PORT:     t.number().default(3000)      // Def<number>
DB_HOST:  t.string().required()         // Def<string>
NODE_ENV: t.enum([...]).required()      // Def<'development'|'production'|'testing'>
API_KEY:  t.string().when('NODE_ENV', 'production', t.required())  // Def<string|undefined>

Type Parameters

T

T

Constructors

Constructor

new Def<T>(spec): Def<T>

Defined in: packages/core/src/env/Def.ts:74

Parameters

spec

FieldSpec

Returns

Def<T>

Methods

required()

required(): Def<NonNullable<T>>

Defined in: packages/core/src/env/Def.ts:89

Mark the field as required. The output type becomes NonNullable<T>undefined is removed.

Returns

Def<NonNullable<T>>

Example

DB_HOST: t.string().required()  // string (throws at boot if missing)

default()

default(value): Def<NonNullable<T>>

Defined in: packages/core/src/env/Def.ts:103

Provide a fallback value when the env variable is absent. The output type becomes NonNullable<T>undefined is removed.

Parameters

value

NonNullable<T>

Returns

Def<NonNullable<T>>

Example

PORT:  t.number().default(3000)         // number — always present
DEBUG: t.boolean().default(false)       // boolean

when()

when(conditionField, conditionValue, rule?): Def<T>

Defined in: packages/core/src/env/Def.ts:127

Make the field conditionally required. When conditionField === conditionValue at runtime, the field is validated as required. Otherwise it remains optional.

The TypeScript type stays T (potentially undefined) because the condition is evaluated at runtime — access the value with a null-check or after calling EnvSchema.define() in the appropriate environment.

Pass t.required() as the third argument (or omit — required is the default action).

Parameters

conditionField

string

conditionValue

string

rule?

typeof _REQUIRED | Def<unknown>

Returns

Def<T>

Example

STRIPE_SECRET: t.string().when('NODE_ENV', 'production', t.required())