I am trying to add a simple generic function to the Array prototype but TypeScript gives me an error about typings that I don't get.
interface Array<T> {
pluck<T, TKey extends keyof T>(this: T[], key: TKey): T[TKey][];
}
Array.prototype.pluck = function pluck<T, TKey extends keyof T>(this: T[], key: TKey): T[TKey][] {
return this.map(item => item[key]);
}
Here is a Codesandbox: https://codesandbox.io/s/typescript-playground-export-3oww1?fontsize=14&hidenavigation=1&theme=dark
Thanks for your help :)