Skip to main content
@spa-tools/utilities

pick()

The pick<T>() function is similar to the TypeScript Pick utility type, but it is used at runtime to return a new object with one or more properties picked from the provided object.

The generic T type is used to define the type of the object that is picked from.

Usage

import { pick } from '@spa-tools/utilities';

const person = {
address: {
city: 'New York',
country: 'USA',
},
childrenAges: [8, 10],
id: 1,
name: 'John',
};

const newPerson = pick(person, 'address', 'name');
console.log(newPerson);
// -->
// {
// address: {
// city: 'New York',
// country: 'USA'
// },
// name: 'John'
// }

Arguments

NameTypeDescription
objTThe object to pick from
...keysstring[]A param list of keys to pick

Returns

Returns object as type of Pick<T, keyof T>.