From 80ff1de3e190c68a59b302e66d4b0a635cfbaace Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Fri, 10 May 2024 19:03:47 +1200 Subject: [PATCH] feat!: undefined will no longer replace defined values by default --- src/defaults/vanilla.ts | 4 ++-- src/types/merging.ts | 4 +++- tests/deepmerge.test-d.ts | 2 +- tests/deepmerge.test.ts | 6 +++--- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/defaults/vanilla.ts b/src/defaults/vanilla.ts index 6c8599409..23fc0cd44 100644 --- a/src/defaults/vanilla.ts +++ b/src/defaults/vanilla.ts @@ -119,8 +119,8 @@ export function mergeMaps< } /** - * Get the last value in the given array. + * Get the last non-undefined value in the given array. */ export function mergeOthers>(values: Ts) { - return values.at(-1); + return values.findLast((value) => value !== undefined); } diff --git a/src/types/merging.ts b/src/types/merging.ts index fbb4aeb9a..6c88f87f8 100644 --- a/src/types/merging.ts +++ b/src/types/merging.ts @@ -9,8 +9,10 @@ import { type EveryIsMap, type EveryIsRecord, type EveryIsSet, + type Is, type IsNever, type IsTuple, + type Or, } from "./utils"; /** @@ -169,7 +171,7 @@ export type DeepMergeLeaf> = : Ts extends readonly [infer T] ? T : Ts extends readonly [...infer Rest, infer Tail] - ? IsNever extends true + ? Or, Is> extends true ? Rest extends ReadonlyArray ? DeepMergeLeaf : never diff --git a/tests/deepmerge.test-d.ts b/tests/deepmerge.test-d.ts index 8c2d651f9..0fcaad121 100644 --- a/tests/deepmerge.test-d.ts +++ b/tests/deepmerge.test-d.ts @@ -163,7 +163,7 @@ const i = { }; const test13 = deepmerge(a, i); -expectType<{ foo: undefined; baz: { quux: string[] }; garply: number }>(test13); +expectType<{ foo: string; baz: { quux: string[] }; garply: number }>(test13); const j = { foo: new Set([1, 2]), diff --git a/tests/deepmerge.test.ts b/tests/deepmerge.test.ts index a5f75c10c..6f799a63f 100644 --- a/tests/deepmerge.test.ts +++ b/tests/deepmerge.test.ts @@ -300,18 +300,18 @@ describe("deepmerge", () => { const x = { key1: undefined }; const y = { key1: { subkey: `one` } }; - const expected = { key1: { subkey: `one` } }; + const expected = { key1: y.key1 }; const merged = deepmerge(x, y); expect(merged).toStrictEqual(expected); }); - it(`replaces records with undefined`, () => { + it(`doesn't replaces records with undefined`, () => { const x = { key1: { subkey: `one` } }; const y = { key1: undefined }; - const expected = { key1: undefined }; + const expected = { key1: x.key1 }; const merged = deepmerge(x, y);