-
-
Notifications
You must be signed in to change notification settings - Fork 625
Fix inconsistent merge onto Destination's optional entries and better handling of undefined vs optional entries #1209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jclaveau
wants to merge
8
commits into
sindresorhus:main
Choose a base branch
from
jclaveau:fix/lost-branch-while-merging-deep-optional-nodes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
22b0e18
fix(MergeDeep): if Destination is optional, Source won't overwrite it…
jclaveau dbf6302
style(MergeDeep): lint #1208
jclaveau 8263a27
wip: experimenting alternative implementation of merging optional key…
jclaveau 4a7934b
Revert "wip: experimenting alternative implementation of merging opti…
jclaveau 4fda6b3
fix(MergeDeep): optional keys VS undefined values working and tested …
jclaveau 969a075
style: typo in comment
jclaveau 32943a9
Update merge-deep.d.ts
sindresorhus 3e66c9f
Update merge-deep.ts
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import {expectType} from 'tsd'; | ||
import {expectTypeOf} from 'expect-type'; | ||
import type {MergeDeep, MergeDeepOptions} from '../index.d.ts'; | ||
|
||
// Test helper. | ||
|
@@ -187,6 +188,129 @@ expectType<MergedFooBar>(mergedFooBar); | |
declare const mergedBarFoo: MergeDeep<FooOptional, BarOptional>; | ||
expectType<MergedFooBar>(mergedBarFoo); | ||
|
||
// Nested Optional: Ensuring the merge goes deep | ||
type OptionalNestedTest = { | ||
Left: { | ||
nested?: { | ||
in_left: string; | ||
sub_nested?: { | ||
in_both?: number; | ||
}; | ||
}; | ||
}; | ||
Right: { | ||
nested: { | ||
in_right: string; | ||
sub_nested: { | ||
in_both: number; | ||
}; | ||
}; | ||
}; | ||
}; | ||
|
||
type OptionalNestedRightIntoLeft = MergeDeep< | ||
OptionalNestedTest['Left'], | ||
OptionalNestedTest['Right'] | ||
>; | ||
expectTypeOf<OptionalNestedRightIntoLeft>().toEqualTypeOf<{ | ||
nested: { // Optional is overwritten by Right | ||
in_left: string; // Subentries are kept in both directions | ||
in_right: string; | ||
sub_nested: { // Optional is overwritten by Right in subentries | ||
in_both: number; | ||
}; | ||
}; | ||
}>(); | ||
|
||
type OptionalNestedLeftIntoRight = MergeDeep< | ||
OptionalNestedTest['Right'], | ||
OptionalNestedTest['Left'] | ||
>; | ||
expectTypeOf<OptionalNestedLeftIntoRight>().toEqualTypeOf<{ | ||
nested?: { // Optional is added by Left | ||
in_left: string; // Subentries are kept in both directions | ||
in_right: string; | ||
sub_nested?: { // Optional is added by Left in subentries | ||
in_both?: number | undefined; | ||
} | undefined; | ||
} | undefined; | ||
// "| undefined" is added here and the only way to remove it | ||
// would depend on exactOptionalPropertyTypes and a complex | ||
// logic which may not worth the effort | ||
}>(); | ||
|
||
// Nested Optional: Optional versus undefined entry | ||
type OptionalOrUndefinedNestedTest = { | ||
Left: { | ||
nested?: { | ||
in_left: string; | ||
}; | ||
}; | ||
Right: { | ||
nested: { | ||
in_right: number; | ||
} | undefined; | ||
}; | ||
}; | ||
|
||
type OptionalOrUndefinedNestedRightIntoLeft = MergeDeep< | ||
OptionalOrUndefinedNestedTest['Left'], | ||
OptionalOrUndefinedNestedTest['Right'] | ||
>; | ||
expectTypeOf<OptionalOrUndefinedNestedRightIntoLeft>().toEqualTypeOf<{ | ||
nested: { // ? is overwritten by Right | ||
in_left: string; | ||
in_right: number; | ||
} | undefined; // Undefined is kept in both directions | ||
}>(); | ||
|
||
type OptionalOrUndefinedNestedRightIntoRight = MergeDeep< | ||
OptionalOrUndefinedNestedTest['Right'], | ||
OptionalOrUndefinedNestedTest['Left'] | ||
>; | ||
expectTypeOf<OptionalOrUndefinedNestedRightIntoRight>().toEqualTypeOf<{ | ||
nested?: { // ? is added by Left | ||
in_left: string; | ||
in_right: number; | ||
} | undefined; // Undefined is kept in both directions | ||
}>(); | ||
|
||
// Nested Optional: Optional versus undefined entry | ||
type OptionalAndUndefinedNestedTest = { | ||
Left: { | ||
nested?: { | ||
in_left: string; | ||
} | undefined; | ||
}; | ||
Right: { | ||
nested: { | ||
in_right: string; | ||
}; | ||
}; | ||
}; | ||
|
||
type OptionalAndUndefinedNestedRightIntoLeft = MergeDeep< | ||
OptionalAndUndefinedNestedTest['Left'], | ||
OptionalAndUndefinedNestedTest['Right'] | ||
>; | ||
expectTypeOf<OptionalAndUndefinedNestedRightIntoLeft>().toEqualTypeOf<{ | ||
nested: { | ||
in_left: string; | ||
in_right: string; | ||
}; // "| undefined" is removed by Right | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Umm... shouldn't type OptionalOrUndefinedNestedRightIntoRight = MergeDeep<
{nested: {in_right: number} | undefined},
{nested?: {in_left: string}}
>;
expectTypeOf<OptionalOrUndefinedNestedRightIntoRight>().toEqualTypeOf<{
nested?: {in_left: string; in_right: number} | undefined;
}>(); |
||
}>(); | ||
|
||
type OptionalAndUndefinedNestedRightIntoRight = MergeDeep< | ||
OptionalAndUndefinedNestedTest['Right'], | ||
OptionalAndUndefinedNestedTest['Left'] | ||
>; | ||
expectTypeOf<OptionalAndUndefinedNestedRightIntoRight>().toEqualTypeOf<{ | ||
nested?: { | ||
in_left: string; | ||
in_right: string; | ||
} | undefined; // "| undefined" is added by Left | ||
}>(); | ||
|
||
// Test for readonly | ||
type ReadonlyFoo = { | ||
readonly string: string; | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally,
| undefined
shouldn't be there.There's a helper called
IsExactOptionalPropertyTypesEnabled
, can you try using that?