I'm trying to add a method to the Array object in Typescript. I've already found other solutions on SO, but none of those work for me.
My code looks like:
interface Array {
    average(): () => number;
}
Array.prototype.average = () => {
    var sum: number = 0
    for (var i = 0; i < this.length; i++)
        sum += this[i]
    if (this.length)
        return sum / this.length
    return 0
}
And I get the error: The property 'average' does not exist on value of type 'Array'
Are you only getting an error in Visual Studio? That much is expected due to a bug in extending build-in interfaces. This should work if you're just invoking tsc.exe.
Relatedly, your code is a little off -- your declaration of average describes a function that returns a function that returns a number, rather than returning a number (you want to just write average(): number on that line). Also, because you used => instead of function() { in the implementation, you'll be binding to the wrong this value at runtime. Hope that helps!
Here is very simple solution (tested with typescript 1.6):
1) Define method with Array type:
interface Array<T> {
    average():number;
}
2) Implement method:
Array.prototype['average'] = function () {
    return this.reduce(function (a, b) {
        if(typeof a !== "number" || typeof b !== "number"){
            throw new Error("avg method applies only on numeric arrays.");
        }
        return a + b;
    }, 0) / this.length;
};
rendered code should be:
Array.prototype.average = function()........
Now you'll be able to call this as:
var arr:number[] = [1, 2, 3, 4, 5, 6];
arr.average()
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With