From 0c3811498d2cc5f04441f85213869ab699f6267d Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Mon, 20 May 2024 17:52:50 +1200 Subject: [PATCH] fix: type when merging index signatures fix #459 --- src/types/defaults.ts | 3 ++- src/types/utils.ts | 8 ++++++++ tests/deepmerge.test-d.ts | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/types/defaults.ts b/src/types/defaults.ts index 18bedd49..8af0f248 100644 --- a/src/types/defaults.ts +++ b/src/types/defaults.ts @@ -7,6 +7,7 @@ import { import { type FilterOutNever, type FlattenTuples, + type KeyIsOptional, type SimplifyObject, type TransposeTuple, type TupleToIntersection, @@ -75,7 +76,7 @@ type RecordToRecordMeta> = { [K in keyof T]-?: { key: K; value: Required[K]; - optional: {} extends Pick ? true : false; + optional: KeyIsOptional; }; }; diff --git a/src/types/utils.ts b/src/types/utils.ts index dd9e8bed..83a049da 100644 --- a/src/types/utils.ts +++ b/src/types/utils.ts @@ -55,6 +55,14 @@ export type Or = T1 extends true */ export type Not = T extends true ? false : true; +/** + * Check if a key is optional in the given object. + */ +export type KeyIsOptional< + K extends PropertyKey, + O extends { [Key in K]?: unknown }, +> = O extends { [Key in K]: unknown } ? false : true; + /** * Returns whether or not all the given types are never. */ diff --git a/tests/deepmerge.test-d.ts b/tests/deepmerge.test-d.ts index f0a62cdd..bb9b9af2 100644 --- a/tests/deepmerge.test-d.ts +++ b/tests/deepmerge.test-d.ts @@ -239,3 +239,8 @@ const p: { a: true; b?: string } = { a: true, b: "n" }; const test18 = deepmerge(o, p); expectType<{ a: true; b?: string | number }>(test18); + +const q: Record = { a: "a" }; + +const test19 = deepmerge(q, q); +expectType>(test19);