Those properties do not actually exist on the ArrayBuffer object. They are put there by the Dev Tools window for viewing the ArrayBuffer contents.
You need to actually create the TypedArray of your choice through its constructor syntax
new TypedArray(buffer [, byteOffset [, length]]);
So in your case if you want Uint8Array you would need to do:
var uint8View = new Uint8Array(arrayBuffer);
Answer from Patrick Evans on Stack Overflowjavascript - Uint8Array to ArrayBuffer - Stack Overflow
How to convert ArrayBuffer to buffer and ArrayButton to uint8 Array in javascript . Please help! - Ask the community - Community Support Forum | Wix Studio
Buffer to Uint8Array conversion method?
You shouldn't be allowed to pass Uint8Array when ArrayBuffer is expected ...
Just using .buffer will not always work, because the buffer may be bigger than the data view.
See this example:
let data = Uint8Array.from([1,2,3,4])
var foobar = data.subarray(0,2)
var arrayBuffer = foobar.buffer;
console.log(new Uint8Array(arrayBuffer)) // will print [1,2,3,4] but we want [1,2]
When using array.buffer it's important use byteOffset and byteLength to compute the actual data
let data = Uint8Array.from([1,2,3,4])
var foobar = data.subarray(0,2)
var arrayBuffer = foobar.buffer.slice(foobar.byteOffset, foobar.byteLength + foobar.byteOffset);
console.log(new Uint8Array(arrayBuffer)) // OK it now prints [1,2]
So here is the function you need:
function typedArrayToBuffer(array: Uint8Array): ArrayBuffer {
return array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset)
}
I found a more simple method to get the ArrayBuffer of Uint8Array.
var arrayBuffer = foobar.buffer;
just this! And it works for me!
It doesn't make sense to allow being able to assign Uint8Array (Uint16Array, Uint32Array, etc.) to an ArrayBuffer.
What's a good way to enforce that with types?
Or maybe there is a type that only accepts ArrayBufer and excludes TypedArrays (like Uint8Array)?
As you can see here, it is currently possible:
https://www.typescriptlang.org/play/?#code/LAKALgngDgpgBAMQPZLgXjgQQE7YIYQBCArgGakzZwwAeYMAdgCYDOcAqgJYNgAcO+CHAD8cAEQQYLMXABc4hkjEBuUJFiIUAJnQdufAQWp1GrLLgIlylEeMnS5CpapCgANjDBwAbvOSoMMUUVd084Bj9tXQkpENcQAGMkBhYvGnlDIjIKKgwGGAB3PR5+CwgACgBtABYtAF0AShcklK8IeS4SzN18osyrHPLapqA
I’m always ending up guessing.. I need to somehow understand. I have no idea what I’m doing when working with files. Just trying stuff until it works. Someone plz ELI5..
My current issue is getting a png file from Firebase storage which gives a “file” which I’m trying to add on a pdf, where the library expects a Uint8Array or Arraybuffer. I’ve been doing similar things before. But end up just guessing my way until it works, and this time.. well it doesn’t work. I guessed it all with no success. I don’t know what I’m doing so I guess I need to actually learn ;)