8

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;
    }

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.