gpt4 book ai didi

angular - 类型 'SharedArrayBuffer' 不可分配给类型 'ArrayBuffer'

转载 作者:太空狗 更新时间:2023-10-29 17:40:11 29 4
gpt4 key购买 nike

我已将我的应用程序从 Angular 4 升级到 Angular 6。我收到以下错误。错误出现在 return data.buffer 行。这是 es 兼容性问题吗?

error TS2322: Type 'ArrayBuffer | SharedArrayBuffer' is not assignable to type 'Arr
ayBuffer'.
Type 'SharedArrayBuffer' is not assignable to type 'ArrayBuffer'.
Types of property '[Symbol.toStringTag]' are incompatible.
Type '"SharedArrayBuffer"' is not assignable to type '"ArrayBuffer"'.

代码

serialize(): ArrayBuffer {
let source: ArrayLike<number>[] = this.contents.map(o => new Uint8Array(o));
let lengths = source.map(o => this.numToArr(o.length));
if (!!this.value) {
const bytes = utf8.toByteArray(JSON.stringify(this.value, null, null));
const dataLength = this.numToArr(bytes.length);
source = [bytes, ...source];
lengths = [dataLength, ...lengths];
}

const totalLength = source.reduce((acc, o) => acc + o.length, 0) + lengths.reduce((acc, o) => acc + o.length, 0);

const data = new Uint8Array(totalLength);
let offset = 0;
source.forEach((item, index) => {
const prefix = lengths[index];
data.set(prefix, offset);
offset += prefix.length;
data.set(item, offset);
offset += item.length;
});

return data.buffer;
}

最佳答案

要么使用如下适当的类型转换:

return data.buffer as ArrayBuffer;

或者定义一个可以接受 ArrayBuffer 或 SharedArrayBuffer 的变量,如下所示:

pdf: ArrayBuffer | SharedArrayBuffer;
...

...
this.pdf = data.buffer;
return this.pdf;

希望这对您有所帮助。

关于angular - 类型 'SharedArrayBuffer' 不可分配给类型 'ArrayBuffer',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52429299/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com