pick

Return a new object containing only the given keys from the source, a type-safe way to project a subset of an object's fields.

Usage

The pick function returns a new object containing only the specified keys.

import { pick } from '@movk/core'

const user = {
  id: 1,
  name: 'John',
  email: 'john@example.com',
  password: 'secret',
  createdAt: '2023-01-01',
  updatedAt: '2023-01-15'
}

const publicInfo = pick(user, ['id', 'name', 'email'])
console.log(publicInfo) // { id: 1, name: 'John', email: 'john@example.com' }

const basicInfo = pick(user, ['id', 'name'])
console.log(basicInfo) // { id: 1, name: 'John' }

API

pick<T, K>(obj, keys)

Returns a new object containing only the specified keys.

Parameters

obj
T extends AnyObject required
The source object.
keys
K[] required
Array of keys to pick.

Returns

returns
PickByKey<T, K>
A new object containing only the specified keys.

Changelog

No recent changes
Copyright © 2024 - 2026 YiXuan - MIT License