Skip to main content

pick

pick() function

Returns a partial copy of an object containing given keys.

Signature:
declare function pick<T extends object, U extends readonly []>(iterable: U, obj: T): Record<string, never>;

declare function pick<T extends object, U extends Iterable<keyof T>>(iterable: U, obj: T): Pick<T, IterableInfer<U>>;

declare function pick<T extends object, U extends Iterable<keyof T>>(iterable: U): (obj: T) => Pick<T, IterableInfer<U>>;

declare function pick<T extends object, U extends AsyncIterable<keyof T>>(iterable: U, obj: T): Promise<Pick<T, IterableInfer<U>>>;

declare function pick<T extends object, U extends AsyncIterable<keyof T>>(iterable: U): (obj: T) => Promise<Pick<T, IterableInfer<U>>>;

Example

const person = {
name: "james",
age: 40,
numberOfKids: 2,
team: "Software Development",
preferredLanguage: "Rust",
};

const dad = pick(["name", "age", "numberOfKids"], person);
// { name: "james", age: 40, numberOfKids: 2 }

const developer = pick(["name", "team", "preferredLanguage"], person);
// { name: "james", team: "Software Development", preferredLanguage: "Rust" }

// with pipe
pipe(
person,
pick(["name", "age", "numberOfKids"]),
);

// if you want to use AsyncIterable as the list of property names
const anonymousDeveloper = await pick(toAsync(["preferredLanguage"] as const), person);

see pipe, toAsync, omit,