Skip to content

Commit

Permalink
fix: type when merging index signatures
Browse files Browse the repository at this point in the history
fix #459
  • Loading branch information
RebeccaStevens committed May 20, 2024
1 parent 46e1c6d commit 5e8b9b6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/types/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {
type FilterOutNever,
type FlattenTuples,
type KeyIsOptional,
type SimplifyObject,
type TransposeTuple,
type TupleToIntersection,
Expand Down Expand Up @@ -75,7 +76,7 @@ type RecordToRecordMeta<T extends Record<PropertyKey, unknown>> = {
[K in keyof T]-?: {
key: K;
value: Required<T>[K];
optional: {} extends Pick<T, K> ? true : false;
optional: KeyIsOptional<K, T>;
};
};

Expand Down
8 changes: 8 additions & 0 deletions src/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export type Or<T1 extends boolean, T2 extends boolean> = T1 extends true
*/
export type Not<T extends boolean> = 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.
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/deepmerge.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = { a: "a" };

const test19 = deepmerge(q, q);
expectType<Record<string, string>>(test19);

0 comments on commit 5e8b9b6

Please sign in to comment.