Extending Object.prototype with TypeScript

后端 未结 3 1949
野的像风
野的像风 2020-12-09 14:59

I am currently working on a TypeScript API, which requires some additional features binding to the Object prototype (Object.prototype).

Consider the following code:<

3条回答
  •  难免孤独
    2020-12-09 15:42

    I used to have:

    // See if an array contains an object
    Array.prototype.contains = function (obj) {
        var i = this.length;
        while (i--) {
            if (this[i] === obj) {
                return true;
            }
        }
        return false;
    }
    

    in order to make that code compile with typescript I added the line:

    interface Array {
        contains(obj: Object): boolean;
    }
    

    Thanks basarat!

提交回复
热议问题