Skip to content

Commit a6af489

Browse files
MergeDeep: Remove extra undefined from optional properties (#1319)
1 parent 0028cc5 commit a6af489

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

source/merge-deep.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type MergeDeepRecordProperty<
3333
Source,
3434
Options extends MergeDeepInternalOptions,
3535
> = undefined extends Source
36-
? MergeDeepOrReturn<Source, Exclude<Destination, undefined>, Exclude<Source, undefined>, Options> | undefined
36+
? MergeDeepOrReturn<Source, Exclude<Destination, undefined>, Exclude<Source, undefined>, Options> | (undefined extends Destination ? undefined : never)
3737
: MergeDeepOrReturn<Source, Destination, Source, Options>;
3838

3939
/**
@@ -58,7 +58,7 @@ type DoMergeDeepRecord<
5858
}
5959
// Case in rule 3: Both the source and the destination contain the key.
6060
& {
61-
[Key in keyof Source as Key extends keyof Destination ? Key : never]: MergeDeepRecordProperty<Destination[Key], Source[Key], Options>;
61+
[Key in keyof Source as Key extends keyof Destination ? Key : never]: MergeDeepRecordProperty<Required<Destination>[Key], Required<Source>[Key], Options>;
6262
};
6363

6464
/**

test-d/merge-deep.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,19 @@ expectType<[Bar, FooBarReplace, ...FooBarReplace[]]>(tupleIntoTupleWithVariadicR
309309

310310
declare const tupleIntoTupleWithVariadicReplaceReversed: MergeDeep<[Foo, ...Foo[]], [number, Bar, ...Bar[]], {arrayMergeMode: 'replace'; recurseIntoArrays: true}>;
311311
expectType<[number, FooBarReplace, ...FooBarReplace[]]>(tupleIntoTupleWithVariadicReplaceReversed);
312+
313+
type RecordNotPartial = {
314+
name: string;
315+
type: string;
316+
b: {bb?: string; cc: string};
317+
};
318+
type RecordPartial = Partial<RecordNotPartial>;
319+
320+
expectType<RecordNotPartial>({} as MergeDeep<RecordPartial, RecordNotPartial>);
321+
expectType<RecordPartial>({} as MergeDeep<RecordNotPartial, RecordPartial>);
322+
323+
type NotOptional = {a: string; b: number; c: boolean};
324+
type OptionalWithUndefined = {a: string | undefined; b?: number; c?: boolean | undefined};
325+
326+
expectType<OptionalWithUndefined>({} as MergeDeep<NotOptional, OptionalWithUndefined>);
327+
expectType<NotOptional>({} as MergeDeep<OptionalWithUndefined, NotOptional>);

0 commit comments

Comments
 (0)