类型“ SharedArrayBuffer”不可分配给“ ArrayBuffer”类型

时间:2018-09-20 16:16:16

标签: angular

我已将应用程序从4号角升级到6号角。出现以下错误。错误是在行返回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;
    }

1 个答案:

答案 0 :(得分:0)

请使用以下适当的类型转换:

return data.buffer as ArrayBuffer;

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

pdf: ArrayBuffer | SharedArrayBuffer;
...

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

希望这会有所帮助。