I want to extend DataView with state and functionality as follows:
export class MyDataView extends DataView
{
public position: number;
public constructor(buffer: ArrayBuffer | SharedArrayBuffer)
{
super(buffer);
}
public readInt32(): number
{
const result = this.getInt32(this.position);
this.position += 4;
return result;
}
}
I then try to create an instance as follows:
const fileSelector = document.getElementById("file-selector");
fileSelector.addEventListener("change", async (ev) =>
{
const fileList = (<HTMLInputElement>ev.target).files;
const arrayBuffer = await fileList[0].arrayBuffer();
const myDataView = new MyDataView(arrayBuffer);
});
However, I get the following error logged in the console:
Uncaught (in promise) TypeError: Constructor DataView requires 'new'
at MyDataView.DataView
at new MyDataView (s. below)
Going to the location marked by it shows the following JS code. I'm not sure if it is correct. In a similar JS-only issue, I saw that super() should be called here instead of _super.call().
function MyDataView(buffer) {
var _this = this;
_this = _super.call(this, buffer) || this;
return _this;
}
How can I properly extend DataView in TypeScript? What am I doing wrong?