sindresorhus/is · error · TypeError

Expected value which is `ArrayBuffer`, received value of typ

Error message

Expected value which is `ArrayBuffer`, received value of type `${is(value)}`.

What it means

Thrown by `assertArrayBuffer(value)` when the value is not an `ArrayBuffer` instance (checked via the library's `isArrayBuffer` object-type tag check). Typed array views, DataViews, Node Buffers, and SharedArrayBuffers all fail this check even though they hold binary data.

Source

Thrown at source/index.ts:1449

	}
}

export function assertArray<T = unknown>(value: unknown, assertion?: (element: unknown, message?: string) => asserts element is T, message?: string): asserts value is T[] {
	if (!isArray(value)) {
		throw new TypeError(message ?? typeErrorMessage('Array', value));
	}

	if (assertion) {
		for (const element of value) {
			// @ts-expect-error: "Assertions require every name in the call target to be declared with an explicit type annotation."
			assertion(element, message);
		}
	}
}

export function assertArrayBuffer(value: unknown, message?: string): asserts value is ArrayBuffer {
	if (!isArrayBuffer(value)) {
		throw new TypeError(message ?? typeErrorMessage('ArrayBuffer', value));
	}
}

export function assertArrayLike<T = unknown>(value: unknown, message?: string): asserts value is ArrayLike<T> {
	if (!isArrayLike(value)) {
		throw new TypeError(message ?? typeErrorMessage('array-like', value));
	}
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export function assertAsyncFunction(value: unknown, message?: string): asserts value is Function {
	if (!isAsyncFunction(value)) {
		throw new TypeError(message ?? typeErrorMessage('AsyncFunction', value));
	}
}

export function assertAsyncGenerator(value: unknown, message?: string): asserts value is AsyncGenerator {
	if (!isAsyncGenerator(value)) {

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. If you have a typed array or Buffer, pass its underlying buffer: `view.buffer` (mind `byteOffset`/`byteLength` — slice if the view doesn't cover the whole buffer).
  2. For fetch, use `await response.arrayBuffer()` rather than the body stream.
  3. If any binary view is acceptable, assert with `assert.typedArray` or `assert.dataView` instead.
  4. Convert strings via `new TextEncoder().encode(str).buffer`.

Example fix

// before
const data = fs.readFileSync(path);
assert.arrayBuffer(data); // Buffer is a Uint8Array view, throws
// after
const buf = fs.readFileSync(path);
assert.arrayBuffer(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(value instanceof ArrayBuffer)) {
  // convert: value = typedArray.buffer or Buffer.buffer slice
}

Type guard

const isArrayBuffer = (value) => value instanceof ArrayBuffer;

Try / catch

try {
  assert.arrayBuffer(value);
} catch (error) {
  if (error instanceof TypeError) { /* got a TypedArray/Buffer/SharedArrayBuffer instead? */ }
  else { throw error; }
}

Prevention

When it happens

Trigger: Passing a `Uint8Array`, `Buffer`, `DataView`, `SharedArrayBuffer`, or a base64/hex string to code that calls `assert.arrayBuffer(value)`. Also cross-realm ArrayBuffers are fine (tag-based check), but a Node `Buffer` is a Uint8Array view, not an ArrayBuffer, and throws.

Common situations: Node.js code passing `fs.readFileSync()` output (a Buffer) where a Web API-style ArrayBuffer is expected; fetch handlers passing `response.body` or a Uint8Array instead of `await response.arrayBuffer()`; WebSocket/crypto APIs mixing views and buffers.

Related errors


AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31). Data as JSON: /data/errors/5958562a784ea712.json. Report an issue: GitHub ↗.