• Merges two objects, preferring values from the first object when both are defined. If a property is undefined in the first object, the value from the second object is used.

    Type Parameters

    • T extends object

      Type of the first object

    • U extends object

      Type of the second object

    Parameters

    • obj1: T

      The first object to merge

    • obj2: U

      The second object to merge

    Returns T & U

    A new object containing all properties from both input objects

    Example

    const obj1 = { a: 1, b: undefined };
    const obj2 = { b: 2, c: 3 };
    const result = mergeObjects(obj1, obj2);
    // result is { a: 1, b: 2, c: 3 }

    Remarks

    • Properties from obj1 take precedence over obj2 when both are defined.
    • The function creates a new object and does not modify the input objects.
    • Nested objects and arrays are not deeply merged, only their references are copied.