Skip to content

defaults

Assigns default values from source to target for properties that are undefined or missing. Existing null, 0, "", and false values in the target are preserved — only undefined triggers replacement.

import { defaults } from "1o1-utils";
import { defaults } from "1o1-utils/defaults";
function defaults({ target, source }: DefaultsParams): Record<string, unknown>
NameTypeRequiredDescription
targetRecord<string, unknown>YesThe base object whose defined values win
sourceRecord<string, unknown>YesThe object supplying defaults

Record<string, unknown> — A new object (neither input is mutated).

defaults({
target: { a: 1 },
source: { a: 99, b: 2 },
});
// => { a: 1, b: 2 }
defaults({
target: { a: null, b: 0, c: "" },
source: { a: 1, b: 99, c: "fallback", d: 4 },
});
// => { a: null, b: 0, c: "", d: 4 }
  • Throws if target is not a plain object.
  • Throws if source is not a plain object.
  • Only replaces values that are strictly undefinednull, 0, "", and false are preserved.
  • Nested objects are copied by reference. Use defaultsDeep for recursive merging.

default values, fill undefined, fallback object, with defaults

I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use defaults to merge a config object with fallback values without overriding explicit nulls