diff --git a/source/merge.d.ts b/source/merge.d.ts index a69ab773d..bed025650 100644 --- a/source/merge.d.ts +++ b/source/merge.d.ts @@ -2,14 +2,10 @@ import type {OmitIndexSignature} from './omit-index-signature'; import type {PickIndexSignature} from './pick-index-signature'; import type {EnforceOptional} from './enforce-optional'; -// Merges two objects without worrying about index signatures or optional keys. +// Merges two objects without worrying about index signatures. type SimpleMerge = { - [Key in keyof Destination | keyof Source]: Key extends keyof Source - ? Source[Key] - : Key extends keyof Destination - ? Destination[Key] - : never; -}; + [Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key]; +} & Source; /** Merge two types into a new type. Keys of the second type overrides keys of the first type. diff --git a/test-d/merge.ts b/test-d/merge.ts index 2f5d1471d..99ae03ae5 100644 --- a/test-d/merge.ts +++ b/test-d/merge.ts @@ -134,3 +134,17 @@ expectType<{ bar: string; fooBar: string; }>(fooBarWithIndexSignature); + +declare const destinationWithAny: Merge<{foo?: any}, {bar: true}>; + +expectType<{ + foo?: any; + bar: true; +}>(destinationWithAny); + +declare const sourceWithAny: Merge<{foo: true}, {bar?: any}>; + +expectType<{ + foo: true; + bar?: any; +}>(sourceWithAny);