sindresorhus/is · error · TypeError

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

Error message

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

What it means

Thrown by `assertUint16Array()` in @sindresorhus/is when the value's toStringTag is not `Uint16Array`. The assert wraps `isUint16Array()` and throws a TypeError with the detected runtime type interpolated so failures fail fast instead of propagating a wrong type.

Source

Thrown at source/index.ts:1926

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

export function assertTupleLike<T extends Array<TypeGuard<unknown>>>(value: unknown, guards: [...T], message?: string): asserts value is ResolveTypesOfTypeGuardsTuple<T> {
	if (!isTupleLike(value, guards)) {
		throw new TypeError(message ?? typeErrorMessage('tuple-like', value));
	}
}

export function assertTypedArray(value: unknown, message?: string): asserts value is TypedArray {
	if (!isTypedArray(value)) {
		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));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Construct the correct view: `new Uint16Array(buffer)` or `Uint16Array.from(array)` before asserting.
  2. Check the producer of the value — typed arrays don't survive JSON serialization; use structuredClone or re-wrap on receipt.
  3. If any typed array is acceptable, use `assert.typedArray()` instead.

Example fix

// before
assert.uint16Array(buffer); // buffer is ArrayBuffer
// after
assert.uint16Array(new Uint16Array(buffer));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!is.uint16Array(value)) {
  value = new Uint16Array(value); // or reject the input
}

Type guard

if (is.uint16Array(value)) {
  assert.uint16Array(value); // never throws
}

Try / catch

try {
  assert.uint16Array(value);
} catch (error) {
  if (error instanceof TypeError) { /* report bad input */ }
  throw error;
}

Prevention

When it happens

Trigger: Calling `assert.uint16Array(value)` / `assertUint16Array(value)` with anything other than a real Uint16Array — e.g. a plain Array of numbers, a Uint8Array, an ArrayBuffer, or a Node Buffer view of a different element size.

Common situations: Passing a raw ArrayBuffer instead of a view over it; decoding binary protocol data and forgetting `new Uint16Array(buffer)`; receiving JSON-serialized data where the typed array became a plain array or `{"0":..}` object.

Related errors


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