ts-deepmerge is a TypeScript utility library providing a robust deep merge function. It automatically infers the return type based on input objects without mutating the source objects, a key differentiator from many mutable merge utilities. Objects and arrays are merged recursively, while primitive values like numbers and strings are overwritten. Merging occurs in the order of arguments provided, giving precedence to later arguments. The current stable version is 7.0.3. As a utility library, its release cadence is driven by new features, bug fixes, or TypeScript version updates rather than a strict schedule. It differentiates itself by strong TypeScript type inference and non-mutation by default, supporting both ESM and CommonJS environments. It also offers an `options` mechanism to customize merge behavior, such as how arrays are handled.
npm install ts-deepmergeVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic deep merging of multiple objects and using `merge.withOptions` to customize array merging behavior.
For generic declared types where the final type is known, explicitly assert the return type using the `as` keyword. For example: `const result = merge(obj1, obj2) as IExpectedType;`
Use `merge.withOptions({ mergeArrays: false }, ...objects)` to configure array merging behavior.No fix needed, this is intended behavior. Be aware that primitive values in later arguments will replace those in earlier arguments at the same path.
This specific error isn't directly related to `ts-deepmerge`'s return type inference but rather how you declare your input objects. Ensure your input objects conform to the expected types, or if you're merging a `Partial` into a complete type, use a type assertion on the *result* of the merge if `ts-deepmerge`'s inference produces a union type that's too broad. Example: `const result = merge(obj1, obj2) as IObj;`
If you know the final type of the array after merging, use a type assertion on the result of the `merge` call: `const result = merge(obj1, obj2) as { myKey: string[] };` or `const myArray = merge(..., { myArray: [1,2] }) as number[];`.No dependency data recorded yet.