diff --git a/source/merge.d.ts b/source/merge.d.ts index 62c222517..f54785658 100644 --- a/source/merge.d.ts +++ b/source/merge.d.ts @@ -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 = Simplify<{ @@ -47,11 +49,14 @@ Note: If you want a merge type that more accurately reflects the runtime behavio export type Merge = Destination extends unknown // For distributing `Destination` ? Source extends unknown // For distributing `Source` - ? Simplify< - SimpleMerge, PickIndexSignature> - & SimpleMerge, OmitIndexSignature> - > + ? If, Destination, _Merge> : never // Should never happen : never; // Should never happen +export type _Merge = + Simplify< + SimpleMerge, PickIndexSignature> + & SimpleMerge, OmitIndexSignature> + >; + export {}; diff --git a/test-d/merge.ts b/test-d/merge.ts index 4af8b1bb1..2cb5dd2d1 100644 --- a/test-d/merge.ts +++ b/test-d/merge.ts @@ -187,3 +187,13 @@ expectType< {[x: number]: number; foo: number} | {bar: boolean; baz: boolean} >, ); + +// Idempotency +type TestIntersectionObject = {a: string} & {b: string}; +// Note: If `Merge` simplified `TestIntersectionObject` to `{a: string; b: string}` then the following test would fail, +// because `expectType` doesn't consider `{a: string; b: string}` equal to `{a: string} & {b: string}`. +expectType({} as Merge); + +// Idempotency: Unions +expectType({} as Merge); +expectType({} as Merge);