There is a method to extend arrays 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 now the question arises, is it possible to extend the array only for a specific type?. For example, only for arrays of type User
-> Array<User>
.
I aim to introduce an extension method, like .toUsersMap()
, which will only be accessible for arrays containing items of type User
.