TypeScript Reference ยท Primitive and Special Types
TypeScript typed arrays (Uint8Array, Float64Array)
Reference for typed arrays in TypeScript: Uint8Array, Int32Array, Float64Array, ArrayBuffer, DataView, overflow behavior and typing byte data safely.
Typed arrays are fixed-length, fixed-element-type views over a block of raw memory. Each numeric format has its own class: Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array, and the BigInt64Array and BigUint64Array pair. Their elements are always numbers (or bigints for the last two), and the values wrap or round to fit the element size.
In TypeScript each class is its own type, and they are not interchangeable even though they look alike. A Uint8Array is the usual type for bytes: file contents, network packets, hashes and encoded text. Recent TypeScript versions made these types generic over the buffer kind (Uint8Array<ArrayBuffer>), which matters when you pass them to APIs that require a specific buffer.
const bytes = new Uint8Array([72, 105]); const samples = new Float32Array(4);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
new Uint8Array(length: number) new Uint8Array(array: ArrayLike<number> | Iterable<number>) new Uint8Array(buffer: ArrayBufferLike, byteOffset?: number, length?: number) interface Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike> { readonly buffer: TArrayBuffer; readonly byteLength: number; readonly byteOffset: number; readonly length: number; [index: number]: number; set(array: ArrayLike<number>, offset?: number): void; subarray(begin?: number, end?: number): Uint8Array<TArrayBuffer>; slice(start?: number, end?: number): Uint8Array<ArrayBuffer>; }
| Name | Description |
|---|---|
length |
Number of elements to allocate, zero-filled. |
array |
Source values, converted element by element to the array's format. |
buffer, byteOffset |
Create a view over existing memory instead of copying. The offset must be a multiple of the element size. |
BYTES_PER_ELEMENT |
Static constant: 1 for 8-bit types, 2, 4 or 8 for the others. |
Returns: subarray returns a view on the same memory; slice returns a copy. Out-of-range reads give undefined at runtime, although the index type is number.
Errors: a misaligned offset or a length that does not fit the buffer throws a RangeError.
Usage notes
- Integer arrays wrap modulo 2^n on assignment;
Uint8ClampedArrayclamps to 0..255 and rounds to nearest instead. Floating arrays never wrap but lose precision (Float32Arraykeeps about 7 decimal digits). - Typed arrays have most array methods (
map,filter,sort,join) but nopush,poporsplice; the length is fixed.sort()without a comparator sorts numerically, unlikeArray.prototype.sort. - Views over the same
ArrayBuffersee each other's writes. Multi-byte values use the machine's byte order (little-endian on nearly all hardware);DataViewlets you choose. - With
noUncheckedIndexedAccess,bytes[0]becomesnumber | undefined, just like normal arrays. - In Node,
Bufferis a subclass ofUint8Array, so aBuffercan be passed wherever aUint8Arrayis expected, but not the other way round. Array.isArray(typedArray)isfalse. UseArrayBuffer.isView(x)orinstanceof.JSON.stringifyof a typed array produces an object with numeric keys, not an array. Convert withArray.fromfirst.- Prefer the narrowest parameter type that works: accept
Uint8Arrayfor bytes, orArrayLike<number>when any numeric sequence will do.