Skip to content

Instantly share code, notes, and snippets.

@theer1k
Last active March 4, 2023 23:12
Show Gist options
  • Save theer1k/85ea81ea8940a4e588197e56b8ed2303 to your computer and use it in GitHub Desktop.
Save theer1k/85ea81ea8940a4e588197e56b8ed2303 to your computer and use it in GitHub Desktop.
[Typescript] - Pick
const pick = <T, K extends keyof T>(obj: T, properties: Array<K>) =>
Object.fromEntries(properties.map((key) => [key, obj[key]])) as Pick<T, K>;
interface ExampleObject {
id: number;
name: string;
birthDate: string;
idol: string;
}
const exampleObject: ExampleObject = {
id: 1,
name: 'Naldo Benny (Michael Jackson 🇧🇷)',
birthDate: '04-19-1979',
idol: 'Chris Brown',
};
const picked = pick(exampleObject, ['name', 'birthDate', 'idol']);
console.log(picked);
// {
// name: 'Naldo Benny (Michael Jackson 🇧🇷)',
// birthDate: '04-19-1979',
// idol: 'Chris Brown'
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment