Issue
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?
Solution
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>;
}
}
Answered By - Fenton
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.