Import mergeDefined, resolveConfig and the ConfigurationValidator<T> type from @neutrium/utilities/configuration or the root export. These helpers add defined-only updates, strict option checking and validated snapshots to native object operations. Use mergeDefined for already-typed updates and resolveConfig for unknown input such as parsed JSON or public API options.
mergeDefined(defaults, ...updates) returns a new mutable object of the defaults' TypeScript type. Updates must be partial values of that type. Later defined values win; undefined inherits the previous value. Null, false, zero and empty strings are explicit replacements. This function does not validate field values at runtime; use resolveConfig at untrusted boundaries.
import { mergeDefined } from '@neutrium/utilities/configuration';
const config = mergeDefined({ precision: 20, enabled: true },
{ precision: undefined }, { enabled: false });
// { precision: 20, enabled: false }
resolveConfig(defaults, input, validate, name?) accepts unknown input, merges it, shallow-freezes the result, and calls an assertion function on the complete result. The assertion must validate every field and any relationships between them. Its errors propagate unchanged. Undefined input means no update. Invalid defaults are also checked, even when no update is supplied. The optional name labels structural errors and defaults to configuration.
import { resolveConfig } from '@neutrium/utilities/configuration';
import { assertKnownKeys, assertPositiveSafeInteger, assertOneOf }
from '@neutrium/utilities/validation';
interface Config { precision: number; mode: 'fast' | 'precise' }
const defaults: Config = { precision: 20, mode: 'fast' };
function validate(value: unknown): asserts value is Config
{
assertKnownKeys(value, ['precision', 'mode']);
assertPositiveSafeInteger(value.precision, 'precision');
assertOneOf(value.mode, ['fast', 'precise'], 'mode');
}
const settings = resolveConfig(defaults, { precision: 30 }, validate);
// Readonly<Config>, shallow-frozen at runtime
Validators are synchronous assertions, not transformations or boolean predicates. JavaScript callers must follow that same contract: returning false does not reject input. Validation cannot modify the frozen outer record and must not mutate nested values. As with any TypeScript assertion, the caller is responsible for truthfully establishing the asserted type.
Defaults define which keys callers may update. Declare an optional key in the runtime object as well as in its TypeScript type:
import { mergeDefined } from '@neutrium/utilities/configuration';
type Options = { label: string | undefined; limits: { entries: number } };
const defaults: Options = { label: undefined, limits: { entries: 128 } };
const updated = mergeDefined(defaults, { label: 'preview', limits: { entries: 32 } });
updated.label; // 'preview'
updated.limits.entries; // 32
const inherited = mergeDefined(updated, { label: undefined });
inherited.label; // 'preview': undefined inherits rather than clearing a value
To support an explicit cleared value, include null in the field's type and
validate it as an allowed value. Nested updates replace the whole nested value;
they do not merge individual nested fields. If callers need an owned, deeply
frozen result, pass the validated result to
immutableSnapshot when all its values are supported data types.
__proto__ key is handled as data and never changes the prototype.The record-copying step is also available independently as copyDataRecord from @neutrium/utilities/validation. It is useful for open-ended maps of data properties where no defaults or merged configuration are needed; see the [validation guide] validation.md).