Reading
config.get(path: string, defaultValue?: any): anyReturns 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"); // -> anyconst 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 value | get(path, "fallback") |
|---|---|
0 | 0 |
"" | "" |
false | false |
null | null |
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")readsobj.api.example.com— three segments — and returns the default. Avoid dots inside keys. config.set("path", undefined)looks like delete but isn’t — it writesnull. JS default parameters substitute forundefined, and the internal signature isset(key, value = null).get("path", "x")returnsnull(which is not undefined, so the default does not kick in). To actually remove a key, useunsetfrom@mongez/reinforcementsagainstconfig.list().- Missing intermediate segments.
config.get("a.b.c", "x")returns"x"if any ofa,b, orcis missing —getshort-circuits the moment it hits a missing key.