sindresorhus/is · error · TypeError

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

Error message

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

What it means

Thrown by `assertDataView()` when the value fails `isDataView` (object type 'DataView'). It ensures code can safely call DataView accessor methods (`getUint32`, `setFloat64`, etc.) for endianness-controlled binary reads.

Source

Thrown at source/index.ts:1538

/**
Note: [Prefer using `Uint8Array` instead of `Buffer`.](https://sindresorhus.com/blog/goodbye-nodejs-buffer)
*/
export function assertBuffer(value: unknown, message?: string): asserts value is NodeBuffer {
	if (!isBuffer(value)) {
		throw new TypeError(message ?? typeErrorMessage('Buffer', value));
	}
}

export function assertClass<T>(value: unknown, message?: string): asserts value is Class<T> {
	if (!isClass(value)) {
		throw new TypeError(message ?? typeErrorMessage('Class', value));
	}
}

export function assertDataView(value: unknown, message?: string): asserts value is DataView {
	if (!isDataView(value)) {
		throw new TypeError(message ?? typeErrorMessage('DataView', value));
	}
}

export function assertDate(value: unknown, message?: string): asserts value is Date {
	if (!isDate(value)) {
		throw new TypeError(message ?? typeErrorMessage('Date', value));
	}
}

export function assertDirectInstanceOf<T>(instance: unknown, class_: Class<T>, message?: string): asserts instance is T {
	if (!isDirectInstanceOf(instance, class_)) {
		throw new TypeError(message ?? typeErrorMessage('T', instance));
	}
}

export function assertEmptyArray(value: unknown, message?: string): asserts value is never[] {
	if (!isEmptyArray(value)) {
		throw new TypeError(message ?? typeErrorMessage('empty array', value));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Wrap the buffer before asserting: `new DataView(arrayBuffer)` or `new DataView(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength)`.
  2. If you only need byte access without endianness control, assert `uint8Array` instead.
  3. Trace the producer to confirm which binary representation it emits and normalize at the boundary.

Example fix

// before
assert.dataView(message.data);
// after
assert.dataView(new DataView(message.data));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(value instanceof DataView)) {
  value = new DataView(value instanceof ArrayBuffer ? value : value.buffer);
}

Type guard

function isDataView(value: unknown): value is DataView {
  return value instanceof DataView;
}

Try / catch

try {
  assert.dataView(value);
} catch (error) {
  if (error instanceof TypeError) {
    // got an ArrayBuffer or typed array — wrap: new DataView(buffer, byteOffset, byteLength)
  } else throw error;
}

Prevention

When it happens

Trigger: Calling `assert.dataView(value)` with a raw ArrayBuffer, a Uint8Array/other typed-array view, a Node Buffer, or a serialized representation of a DataView.

Common situations: Binary protocol parsers that receive `ArrayBuffer` from WebSocket/fetch and forget to wrap it; mixing typed-array views and DataView in the same parsing code; structured-clone/postMessage boundaries delivering the underlying buffer rather than the view.

Related errors


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