Last active
March 4, 2023 23:12
-
-
Save theer1k/85ea81ea8940a4e588197e56b8ed2303 to your computer and use it in GitHub Desktop.
[Typescript] - Pick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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