Skip to content

Instantly share code, notes, and snippets.

@blemoine
Created March 23, 2017 00:23
Show Gist options
  • Save blemoine/47e3e648ed54909ef52b6166a9fa5022 to your computer and use it in GitHub Desktop.
Save blemoine/47e3e648ed54909ef52b6166a9fa5022 to your computer and use it in GitHub Desktop.
function pluck<A, P extends (keyof A)>(array: Array<A>, prop: P): Array<A[P]> {
return array.map(el => el[prop]);
}
pluck([user1, user2], 'firstName') //will be an Array<string>
pluck([user1, user2], 'age') //will be an Array<number>
pluck([user1, user2], 'name') //will not compile Argument of type '"name"' is not assignable to parameter of type '"firstName" | "lastName" | "age"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment