Class

Environment

Environment()

Constructor

# new Environment()

This class extends ObjectAccumulator to merge configuration objects while resolving values from Node or browser environment variables on demand.

An environment accumulator that lazily reads from runtime sources.

sequenceDiagram participant Client participant Env as Environment participant Process as process.env participant Browser as globalThis.ENV Client->>Env: accumulate(partialConfig) Env->>Env: expand(values) Client->>Env: Config.logging.level alt Browser runtime Env->>Browser: lookup ENV key Browser-->>Env: resolved value else Node runtime Env->>Process: lookup ENV key Process-->>Env: resolved value end Env-->>Client: merged value

View Source environment.ts, line 6

Example
const Config = Environment.accumulate({ logging: { level: "info" } });
console.log(Config.logging.level);
console.log(String(Config.logging.level)); // => LOGGING__LEVEL key when serialized

Members

# protected factory

Defines how new instances of the Environment class should be created.

A factory function for creating Environment instances.

View Source environment.ts, line 43

Methods

# expand(value) → {void}

This method defines lazy properties that first consult runtime variables before falling back to seeded values.

Expands an object into the environment.

Parameters:
Name Type Description
value V

The object to expose through environment getters and setters.

View Source environment.ts, line 103

void

# fromEnv(k) → {unknown}

This method handles browser and Node.js environments by normalizing keys and parsing values.

Retrieves a value from the runtime environment.

Parameters:
Name Type Description
k string

The key to resolve from the environment.

View Source environment.ts, line 66

The value that is resolved from the environment, or undefined if it is absent.

unknown

# orThrow() → {EnvironmentInstance.<any>}

Accessing a property that resolves to undefined or an empty string when declared in the model will throw an error.

Returns a proxy that enforces required environment variables.

View Source environment.ts, line 133

A proxy of the environment that enforces required variables.

EnvironmentInstance.<any>

# parseEnvValue(val) → {unknown}

This method interprets booleans and numbers, while leaving other types unchanged.

Converts stringified environment values into native types.

Parameters:
Name Type Description
val unknown

The raw value that is retrieved from the environment.

View Source environment.ts, line 84

The parsed value, converted to a boolean or number, or left as-is.

unknown

# static accumulate(value) → {AccumulatedEnvironment.<any>}

This method adds new properties and hides raw descriptors to avoid leaking enumeration semantics.

Accumulates the given value into the environment.

Parameters:
Name Type Description
value V

The object to merge into the environment.

View Source environment.ts, line 276

The updated environment reference.

AccumulatedEnvironment.<any>

# static buildEnvProxy(current, path) → {any}

This allows chained property access to emit uppercase ENV identifiers, while honoring existing runtime overrides.

Builds a proxy that composes environment keys for nested properties.

Parameters:
Name Type Description
current any

The seed model segment to use when projecting nested structures.

path Array.<string>

The accumulated path segments that lead to the proxy.

View Source environment.ts, line 311

A proxy that resolves environment values or composes additional proxies for deeper paths.

any

# static get(key) → {unknown}

This method delegates to the singleton instance to access stored configuration.

Retrieves a value using a dot-path key from the accumulated environment.

Parameters:
Name Type Description
key string

The key to resolve from the environment store.

View Source environment.ts, line 301

The stored value that corresponds to the provided key.

unknown

# protected static instance(…args) → {E}

This method ensures that only one Environment instance is created, and wraps it in a proxy to compose ENV keys on demand.

Retrieves or creates the singleton instance of the Environment class.

Parameters:
Name Type Attributes Description
args Array.<unknown> <repeatable>

Arguments that are forwarded to the factory when instantiating the singleton.

View Source environment.ts, line 231

The singleton environment instance.

E

# static keys(toEnvopt) → {Array.<string>}

This method gets all keys in the environment, with an option to format them for environment variables.

Retrieves the keys of the environment, optionally converting them to ENV format.

Parameters:
Name Type Attributes Default Description
toEnv boolean <optional>
true

Whether to convert the keys to ENV format.

View Source environment.ts, line 374

An array of keys from the environment.

Array.<string>