Try out this link which includes following TS class that extends another class:
class ExtArray<T> extends Array<T> {
log() {
console.log(this)
}
}
var a = new ExtArray(1,2,3)
a.log()
a.log
should definitely exist, also, TS is able to compile that. However, the JS output fails to invoke ExtArray.prototype.log:
VM107:22 Uncaught TypeError: a.log is not a function
at <anonymous>:22:3
The output:
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var ExtArray = /** @class */ (function (_super) {
__extends(ExtArray, _super);
function ExtArray() {
return _super !== null && _super.apply(this, arguments) || this;
}
ExtArray.prototype.log = function () {
console.log(this);
};
return ExtArray;
}(Array));
var a = new ExtArray(1, 2, 3);
a.log();
What is wrong?