fix: preserve required properties when using onlyDefinedProperties#83
Merged
fix: preserve required properties when using onlyDefinedProperties#83
Conversation
When using `deepmerge({ onlyDefinedProperties: true })`, the runtime
correctly ignores undefined values from the source and preserves target
values. However, the TypeScript types didn't reflect this behavior -
merging `Partial<T>` into `T` would produce a type with optional
properties even though at runtime those undefined values are skipped.
This fix adds new type definitions:
- `DeepMergeDefinedFn` and `DeepMergeAllDefinedFn` function types
- `DeepMergeDefined<T, U>` type that preserves required properties
- `IsMergeableObject<T>` helper that works with Partial types
- Function overloads for `onlyDefinedProperties: true`
Now `deepmerge({ onlyDefinedProperties: true })(fullObj, partialObj)`
correctly returns the full type instead of a partially optional type.
Fixes #82
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Functions should not be treated as mergeable objects. This fixes
an issue where function properties (like callbacks) were being
incorrectly merged as objects, resulting in `{}` instead of
preserving the function type.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #82
When using
deepmerge({ onlyDefinedProperties: true }), the runtime correctly ignores undefined values from the source and preserves target values. However, the TypeScript types didn't reflect this behavior - mergingPartial<T>intoTwould produce a type with optional properties even though at runtime those undefined values are skipped.Before this fix:
After this fix:
Changes
Added new type definitions:
DeepMergeDefinedFnandDeepMergeAllDefinedFnfunction typesDeepMergeDefined<T, U>type that preserves required properties from targetIsMergeableObject<T>helper that works withPartialtypes (the original{ [key: string]: unknown }check fails for optional properties)onlyDefinedProperties: trueAdded comprehensive type tests for:
allmode withonlyDefinedPropertiesTest plan
npm run test:typescript)🤖 Generated with Claude Code