Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions source/merge.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type {OmitIndexSignature} from './omit-index-signature.d.ts';
import type {PickIndexSignature} from './pick-index-signature.d.ts';
import type {Simplify} from './simplify.d.ts';
import type {If} from './if.d.ts';
import type {IsEqual} from './is-equal.d.ts';

// Merges two objects without worrying about index signatures.
type SimpleMerge<Destination, Source> = Simplify<{
Expand Down Expand Up @@ -45,13 +47,15 @@ Note: If you want a merge type that more accurately reflects the runtime behavio
@category Object
*/
export type Merge<Destination, Source> =
Destination extends unknown // For distributing `Destination`
? Source extends unknown // For distributing `Source`
? Simplify<
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
>
: never // Should never happen
: never; // Should never happen
Destination extends unknown // For distributing `Destination`
? Source extends unknown // For distributing `Source`
? If<IsEqual<Destination, Source>, Destination, _Merge<Destination, Source>>
: never // Should never happen
: never; // Should never happen

export type _Merge<Destination, Source> =
Simplify<
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>>;

export {};
26 changes: 24 additions & 2 deletions test-d/merge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import type {Merge} from '../index.d.ts';
import {expectType, expectAssignable} from 'tsd';
import type {Merge, FixedLengthArray} from '../index.d.ts';

type Foo = {
a: number;
Expand Down Expand Up @@ -187,3 +187,25 @@ expectType<
{[x: number]: number; foo: number} | {bar: boolean; baz: boolean}
>,
);

// Idempotency
expectType<BarWithIndexSignatureOverwrite>({} as Merge<BarWithIndexSignatureOverwrite, BarWithIndexSignatureOverwrite>);
expectType<FixedLengthArray<string, 3>>({} as Merge<FixedLengthArray<string, 3>, FixedLengthArray<string, 3>>);

// Idempotency: ensure that `Merge<A, A>` is equal to `A`, where `A` is `{a: t} & {b: t}`
type TestGeneralObject = {a: string; b: string};
type IDMergeGeneral = Merge<TestGeneralObject, TestGeneralObject>;
type TestIntersectionObject = {a: string} & {b: string};
expectType<TestGeneralObject>({} as IDMergeGeneral);
expectType<IDMergeGeneral>({} as TestGeneralObject);
expectType<TestIntersectionObject>({} as Merge<TestIntersectionObject, TestIntersectionObject>);
// There's no problem that this test is not passed.
// See: https://github.com/sindresorhus/type-fest/pull/1336#issuecomment-3804951968
// type IDMergeIntersection = Merge<TestIntersectionObject, TestIntersectionObject>;

expectAssignable<TestIntersectionObject>({a: '1', b: '1'});
expectAssignable<TestGeneralObject>({} as TestIntersectionObject);

// Idempotency: Union Distribution Cases
expectType<TestIntersectionObject | {a: string; b: string; c: string}>({} as Merge<TestIntersectionObject | {c: string}, TestIntersectionObject>);
expectType<TestIntersectionObject | {a: string; b: string; c: string}>({} as Merge<TestIntersectionObject, TestIntersectionObject | {c: string}>);