Instead of utilizing the arguments
keyword, a more TypeScript-friendly approach (and a superior technique in ES2015) is to make use of rest parameters. While I'm not entirely clear on your objective, you can implement rest parameters like so:
addEvent(myArray) {
// By employing an arrow function, you can avoid creating _this
myArray.push = (...args: any[]) => {
Array.prototype.push.apply(myArray, args);
this.onAddItem(arguments);
};
}
After that, calling the addEvent
function would be done as follows:
let arrayLikeObject = new SomeObjecct();
myObj.addEvent(arrayLikeObject);
arrayLikeObject.push(1, 2, 3);
Nonetheless, there seems to be an issue with this methodology. In TypeScript, it's crucial to provide specific type annotations for your parameters. What exactly is the type of myArray
? What types of arguments does it accept? These details should be expressly stated. If it turns out that it accepts a wide range of types, then it might suggest flaws in the design.
UPDATE
Upon comprehending your intentions, it appears that the function declaration should be adjusted to resemble the following format:
(myArray as any[]).push = (...args: any[]): number => {
Array.prototype.push.apply(myArray, args);
this.onAddItem(arguments);
return myArray.length;
};