Skip to content

Commit

Permalink
Add generic for object-merge type (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandredev3 authored Jul 18, 2022
1 parent 1268d78 commit 80b15e1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/object-merge/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Definitions by: nokazn <https://github.com/nokazn>
declare function merge(obj1: object, ...objs: object[]): object;
declare function merge<
TObj1 extends object = object,
TObjs extends object = object
>(obj1: TObj1, ...objs: TObjs[]): TObj1 & TObjs;
export default merge;
9 changes: 9 additions & 0 deletions packages/object-merge/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ merge([]);
merge(() => {});
merge(new Map());
merge(new Set());
merge<{ a: number; b: number; }>({ a: 1, b: 2 });

// Multiple arguments
merge({}, {});
Expand All @@ -18,6 +19,10 @@ merge({}, () => {});
merge({}, new Map());
merge({}, new Set());
merge({}, { a: 1 }, { a: 2, b: 3 });
merge<{ a: number; b: number; }, { a: number; b: number; c: number; }>(
{ a: 4, b: 8 },
{ a: 6, b: 1, c: 8 }
);

// Not OK

Expand All @@ -36,6 +41,8 @@ merge(null);
merge(undefined);
// @ts-expect-error
merge(Symbol());
// @ts-expect-error
merge<{ a: string; b: string; }>({ a: 1, b: 2 });

// Multiple arguments
// @ts-expect-error
Expand All @@ -52,3 +59,5 @@ merge({}, undefined);
merge({}, Symbol());
// @ts-expect-error
merge({}, {}, null);
// @ts-expect-error
merge<{ a: string; b: string; }, { a: string; b: string; c: string; }>({ a: 4, b: 8 }, { a: 6, b: 1, c: 8 });

0 comments on commit 80b15e1

Please sign in to comment.