Skip to content

Commit

Permalink
fix: correct pick and omit type definitions
Browse files Browse the repository at this point in the history
Closes #26
  • Loading branch information
nikku authored and fake-join[bot] committed Apr 27, 2023
1 parent 99e069a commit d292d87
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/object.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
type PropertyName = string | number | symbol;

/**
* Copy the values of all of the enumerable own properties from one or more source objects to a
* target object. Returns the target object.
Expand Down Expand Up @@ -59,7 +61,17 @@ export function get(target: any, path: (string|number)[], defaultValue?: any): a
*
* @return the element
*/
export function set<T>(target: T, path: (string|number)[], value: any): T;
export function set<T>(target: T, path: PropertyName[], value: any): T;

/**
* Pick properties from the given target.
*
* @param target
* @param properties
*
* @return
*/
export function pick<T, V extends keyof T>(target: T, properties: Array<V>): Pick<T, V>;

/**
* Pick properties from the given target.
Expand All @@ -69,7 +81,17 @@ export function set<T>(target: T, path: (string|number)[], value: any): T;
*
* @return
*/
export function pick<T, V extends any[]>(target: T, properties: V): Pick<T, V>;
export function pick<T, V extends PropertyName[]>(target: T, properties: V): Partial<T>;

/**
* Pick all target properties, excluding the given ones.
*
* @param target
* @param properties
*
* @return target
*/
export function omit<T, V extends keyof T>(target: T, properties: V): Omit<T, V>;

/**
* Pick all target properties, excluding the given ones.
Expand All @@ -79,7 +101,7 @@ export function pick<T, V extends any[]>(target: T, properties: V): Pick<T, V>;
*
* @return target
*/
export function omit<T, V extends any[]>(target: T, properties: V): Omit<T, V>;
export function omit<T, V extends PropertyName[]>(target: T, properties: V): Pick<T, Exclude<keyof T, V[number]>>;

/**
* Copy the values of all of the enumerable own properties from one or more source objects to a
Expand Down
5 changes: 5 additions & 0 deletions test/object.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ describe('object', function() {
e: undefined
});

// and when
let otherPicked = pick(obj, [ 'a', 'b' ]);

// then
expect(otherPicked.a).to.eql(1);
});


Expand Down

0 comments on commit d292d87

Please sign in to comment.