-
-
Save blemoine/47e3e648ed54909ef52b6166a9fa5022 to your computer and use it in GitHub Desktop.
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
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