Skip to content

Commit

Permalink
fix: return type when using empty records
Browse files Browse the repository at this point in the history
fix #465
  • Loading branch information
RebeccaStevens committed May 20, 2024
1 parent f4991d8 commit 6b4ff3f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/types/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,20 @@ type RecordPropertyMeta<

type RecordsToRecordMeta<
Ts extends ReadonlyArray<Record<PropertyKey, unknown>>,
> = {
> = FilterOutNever<{
[I in keyof Ts]: RecordToRecordMeta<Ts[I]>;
};
}>;

type RecordToRecordMeta<T extends Record<PropertyKey, unknown>> = {
[K in keyof T]-?: {
key: K;
value: Required<T>[K];
optional: KeyIsOptional<K, T>;
};
};
type RecordToRecordMeta<T extends Record<PropertyKey, unknown>> =
object extends T
? never
: {
[K in keyof T]-?: {
key: K;
value: Required<T>[K];
optional: KeyIsOptional<K, T>;
};
};

/**
* Deep merge records.
Expand All @@ -98,10 +101,13 @@ export type DeepMergeRecordsDefaultHKT<
* Deep merge record props.
*/
type DeepMergeRecordMetaDefaultHKTProps<
RecordMetas extends ReadonlyArray<RecordMeta>,
RecordMetas,
MF extends DeepMergeMergeFunctionsURIs,
M,
> = CreateRecordFromMeta<MergeRecordMeta<RecordMetas>, MF, M>;
> =
RecordMetas extends ReadonlyArray<RecordMeta>
? CreateRecordFromMeta<MergeRecordMeta<RecordMetas>, MF, M>
: never;

type MergeRecordMeta<RecordMetas extends ReadonlyArray<RecordMeta>> =
GroupValuesByKey<
Expand Down
9 changes: 9 additions & 0 deletions tests/deepmerge.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,12 @@ const q: Record<string, string> = { a: "a" };

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

const test20 = deepmerge({}, a);
expectType<{
foo: string;
baz: {
quux: string[];
};
garply: number;
}>(test20);

0 comments on commit 6b4ff3f

Please sign in to comment.