How to add a method to a base type, say Array? In the global module this will be recognized
interface Array { remove(o): Array; }
but where to put the actual implementation?
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>; } }
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