Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'SharedArrayBuffer' is not assignable to type 'ArrayBuffer'

Tags:

angular

I have upgraded my application from angular 4 to angular 6. I am getting the following error. The error is at the line return data.buffer. Is this a es compatibility issue ?

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"'.

code

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;
    }
like image 933
Tom Avatar asked Nov 08 '22 02:11

Tom


1 Answers

Either use appropriate type castings as below:

return data.buffer as ArrayBuffer;

Or Define a variable that can accept either an ArrayBuffer or a SharedArrayBuffer, as below:

pdf: ArrayBuffer | SharedArrayBuffer;
...

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

Hope this helps.

like image 50
Priya Chandrasekhar Avatar answered Nov 14 '22 23:11

Priya Chandrasekhar