From 398d0a48bda0831303843ac2a7b027f7e3d51969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Mischler?= Date: Tue, 28 Mar 2023 18:03:28 +0200 Subject: [PATCH 1/2] Fix `Merge` with optional `any` value --- source/merge.d.ts | 11 ++++------- test-d/merge.ts | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/source/merge.d.ts b/source/merge.d.ts index a69ab773d..a72151dd2 100644 --- a/source/merge.d.ts +++ b/source/merge.d.ts @@ -1,15 +1,12 @@ import type {OmitIndexSignature} from './omit-index-signature'; import type {PickIndexSignature} from './pick-index-signature'; import type {EnforceOptional} from './enforce-optional'; +import type {Simplify} from './simplify'; -// 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); From cbb7bbe37d62dab116a5fb7189edd211ac295b82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Mischler?= Date: Tue, 28 Mar 2023 19:57:25 +0200 Subject: [PATCH 2/2] fix: remove unused import --- source/merge.d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/source/merge.d.ts b/source/merge.d.ts index a72151dd2..bed025650 100644 --- a/source/merge.d.ts +++ b/source/merge.d.ts @@ -1,7 +1,6 @@ import type {OmitIndexSignature} from './omit-index-signature'; import type {PickIndexSignature} from './pick-index-signature'; import type {EnforceOptional} from './enforce-optional'; -import type {Simplify} from './simplify'; // Merges two objects without worrying about index signatures. type SimpleMerge = {