Skip to content

pick

Returns a new object or array containing the specified keys or indices extracted from the input object or array. The return value is a plain object or array and does not modify the original input.

Added in v0.0.1

Usage

typescript
import { pick } from 'parsnip-kit'

const obj = { a: 1, b: 2, c: 3 }
const keys0 = ['a', 'c'] as const
const result0 = pick(obj, keys0)
// Pick<{ a: number; b: number; c: number; }, "a" | "c">
// { a: 1, c: 3 }

const arr = [1, 2, 3, 4]
const keys1 = ['1', '[3]'] as const
const result1 = pick(obj, keys1)
// Pick<number[], 1 | 3>
// [2, 4]

API

Type Parameter

ArgTypeDescription
Textends objectType of the object to be processed
Rextends readonly string[]Array type of field paths

Arguments

ArgTypeOptionalDefaultDescription
objTfalseundefinedType of the object to be processed
keysRfalseundefinedThe keys or array indices to extract

Returns

Type
Pick<T, KeyOrIndex<ExtractUnion<R>> & keyof T>

Reference

KeyOrIndex ExtractUnion