我想以以下方式扩展DataView的状态和功能:
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;
}
}然后我尝试创建一个实例,如下所示:
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);
});但是,我在控制台中记录了以下错误:
Uncaught (in promise) TypeError: Constructor DataView requires 'new'
at MyDataView.DataView
at new MyDataView (s. below)转到由它标记的位置,将显示以下JS代码。我不确定它是否正确。在similar JS-only issue中,我看到super()应该在这里被调用,而不是_super.call()。
function MyDataView(buffer) {
var _this = this;
_this = _super.call(this, buffer) || this;
return _this;
}如何正确地在DataView中扩展TypeScript?我做错了什么?
发布于 2022-11-30 23:50:55
您的tsconfig.json中的“目标”可能设置为" ES5“,并且没有ES5方法来扩展ES6类,比如Map、Set或DataView。
(资料来源:https://github.com/microsoft/TypeScript/issues/10853#issuecomment-246211140)
对我有用的解决方案是,我将"target“设置为"es6”,然后转到代码中:
var MyClass = /** @class */ (function (_super) {
__extends(BinaryIO, _super);
function BinaryIO(buffer, byteOffset, byteLength) {
var _this = _super.call(this, buffer, byteOffset, byteLength) || this;
_this.position = 0;
return _this;
}
...
}至
class MyClass extends DataView {
constructor(buffer, byteOffset, byteLength) {
super(buffer, byteOffset, byteLength);
this.position = 0;
}
...
}https://stackoverflow.com/questions/62613064
复制相似问题