I know how to extend array for any type:
declare global {
interface Array<T> {
remove(elem: T): Array<T>;
}
}
if (!Array.prototype.remove) {
Array.prototype.remove = function<T>(this: T[], elem: T): T[] {
return this.filter(e => e !== elem);
}
}
Source: Extending Array in TypeScript
But is there also a way to extend the array only for a specific type?. Like only for arrays of type User
-> Array<User>
.
I want to create a extend method, like .toUsersMap()
and this method should not be displayed for arrays, which have not the type User
.