denoland/deno · error · TypeError

ERR_INVALID_ARG_TYPE

ERR_INVALID_ARG_TYPE

Error message

The "view" argument must be of type TypedArray. Received ${view}

What it means

Buffer.copyBytesFrom(view, offset, length) requires its first argument to be a TypedArray and throws ERR_INVALID_ARG_TYPE('view', ['TypedArray'], view) at buffer.mjs:342 when it is not. Common confusion: ArrayBuffer and DataView are rejected (they are not TypedArrays), while Buffer itself is accepted because it subclasses Uint8Array. The API copies bytes from a typed-array view into a new Buffer without copying the source twice.

Source

Thrown at ext/node/polyfills/internal/buffer.mjs:342

    value,
  );
}

const BufferFrom = Buffer.from = function from(
  value,
  encodingOrOffset,
  length,
) {
  return _from(value, encodingOrOffset, length);
};

Buffer.copyBytesFrom = function copyBytesFrom(
  view,
  offset,
  length,
) {
  if (!isTypedArray(view)) {
    throw new ERR_INVALID_ARG_TYPE("view", ["TypedArray"], view);
  }

  const viewLength = TypedArrayPrototypeGetLength(view);
  if (viewLength === 0) {
    return Buffer.alloc(0);
  }

  if (offset !== undefined || length !== undefined) {
    if (offset !== undefined) {
      validateInteger(offset, "offset", 0);
      if (offset >= viewLength) return Buffer.alloc(0);
    } else {
      offset = 0;
    }
    let end;
    if (length !== undefined) {
      validateInteger(length, "length", 0);
      end = offset + length;

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Wrap non-view inputs: pass new Uint8Array(arrayBuffer) instead of the ArrayBuffer itself
  2. Type-guard at the call site: ArrayBuffer.isView(view) && !(view instanceof DataView)
  3. For plain arrays of bytes use Buffer.from(arr) instead of copyBytesFrom
  4. For DataView sources, construct new Uint8Array(dv.buffer, dv.byteOffset, dv.byteLength)

Example fix

// before
const copy = Buffer.copyBytesFrom(await file.arrayBuffer());

// after
const copy = Buffer.copyBytesFrom(new Uint8Array(await file.arrayBuffer()));
Defensive patterns

Strategy: type-guard

Validate before calling

const isTypedArray = (v) =>
  ArrayBuffer.isView(v) && !(v instanceof DataView);

if (!isTypedArray(source)) {
  source = new Uint8Array(source); // wraps ArrayBuffer; throws on invalid input
}
const copy = Buffer.copyBytesFrom(source, offset, length);

Type guard

const isTypedArray = (v) =>
  ArrayBuffer.isView(v) && !(v instanceof DataView);

Prevention

When it happens

Trigger: Buffer.copyBytesFrom(arrayBuffer) - passing the underlying ArrayBuffer instead of a view over it; Buffer.copyBytesFrom(new DataView(buf)) - DataView fails isTypedArray; Buffer.copyBytesFrom([1, 2, 3]) or a plain object with a length - ordinary arrays are rejected; a loosely typed function parameter receiving whatever the caller had handy.

Common situations: Interfacing with Web APIs that hand back ArrayBuffers (File.arrayBuffer(), crypto.subtle) and forwarding them directly; mixed DataView/TypedArray codebases; converting wasm memory slices.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/34fa8672c84fcf5f. Report an issue: GitHub.