Skip to content

Commit 02b412b

Browse files
fix: Merge, return arg if both are equal
Co-authored-by: Som Shekhar Mukherjee <49264891+som-sm@users.noreply.github.com>
1 parent d513e9d commit 02b412b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

source/merge.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type {OmitIndexSignature} from './omit-index-signature.d.ts';
22
import type {PickIndexSignature} from './pick-index-signature.d.ts';
33
import type {Simplify} from './simplify.d.ts';
4+
import type {If} from './if.d.ts';
5+
import type {IsEqual} from './is-equal.d.ts';
46

57
// Merges two objects without worrying about index signatures.
68
type SimpleMerge<Destination, Source> = Simplify<{
@@ -44,7 +46,9 @@ Note: If you want a merge type that more accurately reflects the runtime behavio
4446
@see {@link ObjectMerge}
4547
@category Object
4648
*/
47-
export type Merge<Destination, Source> =
49+
export type Merge<Destination, Source> = If<IsEqual<Destination, Source>, Destination, _Merge<Destination, Source>>;
50+
51+
export type _Merge<Destination, Source> =
4852
Destination extends unknown // For distributing `Destination`
4953
? Source extends unknown // For distributing `Source`
5054
? Simplify<

test-d/merge.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expectType} from 'tsd';
2-
import type {Merge, FixedLengthArray} from '../index.d.ts';
2+
import type {Merge, FixedLengthArray, IsEqual} from '../index.d.ts';
33

44
type Foo = {
55
a: number;
@@ -191,3 +191,9 @@ expectType<
191191
// Idempotency
192192
expectType<BarWithIndexSignatureOverwrite>({} as Merge<BarWithIndexSignatureOverwrite, BarWithIndexSignatureOverwrite>);
193193
expectType<FixedLengthArray<string, 3>>({} as Merge<FixedLengthArray<string, 3>, FixedLengthArray<string, 3>>);
194+
195+
// Idempotency
196+
type TestIntersectionObject = {a: string} & {b: string};
197+
type IDMerge = Merge<TestIntersectionObject, TestIntersectionObject>;
198+
expectType<TestIntersectionObject>({} as IDMerge);
199+
expectType<IDMerge>({} as TestIntersectionObject);

0 commit comments

Comments
 (0)