Skip to content

Commit

Permalink
fix: return type of merge result
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 19, 2022
1 parent bb7bda3 commit cc534b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
* view the LICENSE file that was distributed with this source code.
*/

export type MergerResult<A, B> = A extends B ? B extends A ? A : (A & B) : (A & B);
export type MergerResult<A, B> = A extends B ?
B extends A ?
// eslint-disable-next-line @typescript-eslint/ban-types
(A extends {} ? B : A) :
(A & B) :
(A & B);

export type Merger = <A extends Record<string, any>, B extends Record<string, any>>(target: A, ...sources: B[]) => MergerResult<A, B>;

export type Options = {
Expand Down
11 changes: 11 additions & 0 deletions test/unit/module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,16 @@ describe('src/module/*.ts', () => {
let merger = createMerger({priority: 'right'});
const merged = merger( {prototype: null}, {prototype: 1});
expect(merged).toEqual({prototype: null})
});

it('should return optimized return type', () => {
let item : Record<string, any> = {
id: 1,
name: 'admin'
}

let data = merge({}, item);
expect(data.id).toEqual(1);
expect(data.name).toEqual('admin');
})
})

0 comments on commit cc534b9

Please sign in to comment.