Skip to content

@kaelyx/ts-kit / object / pick

Function: pick() ​

pick<T, K>(object, keys): Pick<T, K>

Defined in: object/pick/pick.ts:24

Creates a new object by picking the specified keys from the source object.

Behaves like destructuring: pick(object, ['a', 'b']) reads the same values as const { a, b } = object. Own, inherited and non-enumerable properties are all picked, and getters are read so that their current value is copied. Keys that are not present on the source object or its prototype chain are left out of the output.

Type Parameters ​

T ​

T extends object

K ​

K extends string | number | symbol

Parameters ​

object ​

T

The source object. An empty object will result in an empty output.

keys ​

readonly K[]

The keys to pick from the source object. An empty array will result in an empty output.

Returns ​

Pick<T, K>

A new object containing only the specified keys that are present on the input object.

Example ​

ts
pick({ a: 1, b: 2 }, ['a']) // result: { a: 1 }
pick({ a: 1, b: 2, c: 3 }, ['a', 'c']) // result: { a: 1, c: 3 }
pick({ a: 1, b: undefined }, ['b']) // result: { b: undefined }
pick({ a: 1, b: 2 }, []) // result: {}