5

in the post of "Typescript extend String Static", I got the a few that we can extend existing baseclass of typescript, for example, add new method

interface StringConstructor {
   isNullOrEmpty(str:string):boolean;
}
String.isNullOrEmpty = (str:string) => !str;

it really does work. but for generic interface, I met problems. for example, I need to add new method contain() in Array. I use the following code:

   //1
    interface Array<T> {
        contain(item: T): boolean;
    }  
    //2
    ?????? = (item: T) => {
    // ....
        return true;
    };

after step1, in VS intellisense does and pop up contain method, but where can I do implement method?

Community
  • 1
  • 1
VinceDan
  • 71
  • 1
  • 4
  • Possible duplicate of [Extending Array in TypeScript](http://stackoverflow.com/questions/12802383/extending-array-in-typescript) – Rikki Gibson Feb 28 '17 at 19:19

1 Answers1

7

As the definition in the interface is already bound to the generic constraint, in the implementation you can just treat it as any:

interface Array<T> {
    contain(item: T): boolean;
}  

Array.prototype.contain = function(item) {
    return this.some(obj => item == obj);
};

Also, do not use arrow functions for prototype methods, here's why:

interface Array<T> {
    fn(): void;
}

Array.prototype.fn = () => {
    console.log(this);
};

let a = [];
a.fn(); // Window

But:

Array.prototype.fn = function() {
    console.log(this);
};

let a = [];
a.fn(); // []

If you're targeting es5 or lower then it doesn't matter as the compiler translates arrow functions into regular ones, but if you will then switch to targeting es6 (and the arrow functions will persist) then your code will break and you won't understand why.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • hi Nitzan, thanks your answer... if implement it as a instance method, it should be the same as in .net framework. I found whether use arrow function or not, the intellisense hint the parameter item is any, and no array itself which must be used in method body.. any suggestion about get array in method body? – VinceDan Jan 15 '17 at 14:10
  • Is it supposed to be an instance method or a static method? It makes sense based on the signature you posted that it's an instance method. The item is what you're looking for inside the array no? If so, it is supposed to be `any` – Nitzan Tomer Jan 15 '17 at 14:14
  • I like to do it by instance method because I am .net developer..:-) my concern is in method body, I can not get array[], I need array[] and item which has been passed in to write contains logic... – VinceDan Jan 15 '17 at 14:39
  • The array is just `this` because you are adding this method to the `Array` object prototype. As in my example: `return this.some(obj => item == obj)`. The `this.some` method is the `Array.prototype.some` method. – Nitzan Tomer Jan 15 '17 at 14:43
  • got it. all I do is based on vs intellisense, but it does not work here...thanks again Nitzan.. – VinceDan Jan 15 '17 at 14:51
  • It should work, otherwise you have problems with your IDE. [Check it out in playground](https://www.typescriptlang.org/play/index.html#src=interface%20Array%3CT%3E%20%7B%0D%0A%20%20%20%20contain(item%3A%20T)%3A%20boolean%3B%0D%0A%7D%20%20%0D%0AArray.prototype.contain%20%3D%20function(item)%20%7B%0D%0A%20%20%20%20return%20this.some(obj%20%3D%3E%20item%20%3D%3D%20obj)%3B%0D%0A%7D%3B) – Nitzan Tomer Jan 15 '17 at 15:56