Skip to content

Reading

config.get(path: string, defaultValue?: any): any

Returns the value at path. Substitutes defaultValue for missing paths or paths that terminate in undefined.

Signatures

config.get("api.url"); // -> any (null when missing — the default default)
config.get("api.url", "https://default"); // -> any
const timeout: number = config.get("api.timeout", 30000);

config.get is not a TypeScript generic — its return type is any. Narrow at the call site by annotating the receiving variable, asserting (config.get("api.url") as string), or wrapping (see mongez-config-typing).

Dot-notation

Paths are split on .. Each segment is a key into the value at the previous step:

config.set({
api: {
url: "https://api.example.com",
headers: { "x-app-id": "web" },
},
});
config.get("api.url"); // "https://api.example.com"
config.get("api.headers.x-app-id"); // "web"

Numeric segments index arrays

config.set({ servers: ["primary", "secondary", "fallback"] });
config.get("servers.0"); // "primary"
config.get("servers.2"); // "fallback"
config.get("servers.5", "n/a"); // "n/a"

Default-substitution rules

defaultValue is returned ONLY when the path is missing or terminates in undefined. Other falsy values pass through:

Stored valueget(path, "fallback")
00
""""
falsefalse
nullnull
undefined"fallback"
missing path"fallback"
config.set("flag.enabled", false);
config.get("flag.enabled", true); // false (NOT true)
config.set("count", 0);
config.get("count", 1); // 0
config.set("name", "");
config.get("name", "Anonymous"); // ""

If you need “falsy → default” semantics, do it at the call site: config.get("flag", false) || true.

The default for defaultValue is null

config.get("never.set"); // null (NOT undefined)

This is helpful for ?? patterns: const url = config.get("api.url") ?? "https://default".

Gotchas

  • Dots in keys. If you stored a key with a literal dot ({"api.example.com": 1}), get("api.example.com") reads obj.api.example.com — three segments — and returns the default. Avoid dots inside keys.
  • config.set("path", undefined) looks like delete but isn’t — it writes null. JS default parameters substitute for undefined, and the internal signature is set(key, value = null). get("path", "x") returns null (which is not undefined, so the default does not kick in). To actually remove a key, use unset from @mongez/reinforcements against config.list().
  • Missing intermediate segments. config.get("a.b.c", "x") returns "x" if any of a, b, or c is missing — get short-circuits the moment it hits a missing key.