Extending Array in TypeScript

You can use the prototype to extend Array:

interface Array<T> {
    remove(o: T): Array<T>;
}

Array.prototype.remove = function (o) {
    // code to remove "o"
    return this;
}

If you are within a module, you will need to make it clear that you are referring to the global Array<T>, not creating a local Array<T> interface within your module:

declare global {
    interface Array<T> {
        remove(o: T): Array<T>;
    }
}

Leave a Comment