sindresorhus/is · error · TypeError

Expected value which is `Uint8Array`, received value of type

Error message

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

What it means

Thrown by `assertUint8Array()` in @sindresorhus/is when the value fails the `isUint8Array` check. Note that Node.js Buffer IS a Uint8Array subclass and passes; plain arrays, ArrayBuffers, and other views do not.

Source

Thrown at source/index.ts:1938

		throw new TypeError(message ?? typeErrorMessage('TypedArray', value));
	}
}

export function assertUint16Array(value: unknown, message?: string): asserts value is Uint16Array {
	if (!isUint16Array(value)) {
		throw new TypeError(message ?? typeErrorMessage('Uint16Array', value));
	}
}

export function assertUint32Array(value: unknown, message?: string): asserts value is Uint32Array {
	if (!isUint32Array(value)) {
		throw new TypeError(message ?? typeErrorMessage('Uint32Array', value));
	}
}

export function assertUint8Array(value: unknown, message?: string): asserts value is Uint8Array {
	if (!isUint8Array(value)) {
		throw new TypeError(message ?? typeErrorMessage('Uint8Array', value));
	}
}

export function assertUint8ClampedArray(value: unknown, message?: string): asserts value is Uint8ClampedArray {
	if (!isUint8ClampedArray(value)) {
		throw new TypeError(message ?? typeErrorMessage('Uint8ClampedArray', value));
	}
}

export function assertUndefined(value: unknown, message?: string): asserts value is undefined {
	if (!isUndefined(value)) {
		throw new TypeError(message ?? typeErrorMessage('undefined', value));
	}
}

export function assertUrlInstance(value: unknown, message?: string): asserts value is URL {
	if (!isUrlInstance(value)) {
		throw new TypeError(message ?? typeErrorMessage('URL', value));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Wrap ArrayBuffers: `new Uint8Array(arrayBuffer)`.
  2. Decode strings first: `new TextEncoder().encode(str)` or `Buffer.from(str, 'base64')`.
  3. For Blobs, await `blob.bytes()` (or `new Uint8Array(await blob.arrayBuffer())`).

Example fix

// before
const data = await response.arrayBuffer();
assert.uint8Array(data);
// after
const data = new Uint8Array(await response.arrayBuffer());
assert.uint8Array(data);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!is.uint8Array(value)) {
  value = value instanceof ArrayBuffer ? new Uint8Array(value) : Uint8Array.from(value);
}

Type guard

if (is.uint8Array(value)) {
  // value is Uint8Array here
}

Try / catch

try {
  assert.uint8Array(value);
} catch (error) {
  if (error instanceof TypeError) { /* handle non-Uint8Array */ }
  throw error;
}

Prevention

When it happens

Trigger: `assert.uint8Array(value)` with an ArrayBuffer, a plain array of bytes, a base64 string, a DataView, or a Blob.

Common situations: Web crypto / fetch code where `response.arrayBuffer()` returns an ArrayBuffer that was passed on unwrapped; file contents read as string instead of buffer; byte data arriving base64-encoded from an API.

Related errors


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