Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return type when using empty records #466

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Loading