From 60a057b891f94df3d6ffe3d10fd6b90d475d9315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Mischler?= Date: Wed, 29 Mar 2023 14:38:09 +0200 Subject: [PATCH] Fix `Merge` with optional `any` value (#583) --- source/merge.d.ts | 10 +++------- test-d/merge.ts | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) 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);