zloirock/core-js · error · TypeError

ArrayBuffer expected

Error message

ArrayBuffer expected

What it means

core-js's arrayBufferByteLength helper returns the byteLength of an ArrayBuffer, falling back to a manual implementation when the native getter is unavailable. In the fallback it first verifies the argument's internal class is 'ArrayBuffer'; otherwise it throws 'TypeError: ArrayBuffer expected'. The spec step this mirrors is RequireInternalSlot(O, [[ArrayBufferData]]), so it fires when you pass something that is not an ArrayBuffer (note shared ArrayBuffers are rejected elsewhere).

Source

Thrown at packages/core-js/internals/array-buffer-byte-length.js:13

'use strict';
var globalThis = require('../internals/global-this');
var uncurryThisAccessor = require('../internals/function-uncurry-this-accessor');
var classof = require('../internals/classof-raw');

var ArrayBuffer = globalThis.ArrayBuffer;
var TypeError = globalThis.TypeError;

// Includes
// - Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
// - If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
module.exports = ArrayBuffer && uncurryThisAccessor(ArrayBuffer.prototype, 'byteLength', 'get') || function (O) {
  if (classof(O) !== 'ArrayBuffer') throw new TypeError('ArrayBuffer expected');
  return O.byteLength;
};

View on GitHub (pinned to 84e45fba09)

Solutions

  1. Pass an actual ArrayBuffer instance (use typedArray.buffer when starting from a typed array).
  2. Verify the value with Object.prototype.toString.call(v) === '[object ArrayBuffer]' before calling.
  3. If you hold a SharedArrayBuffer, switch to a regular ArrayBuffer — the spec explicitly rejects shared buffers here.
  4. Ensure a consistent core-js version/config; mismatched polyfill setups can route to the fallback path unexpectedly.

Example fix

// before
var len = arrayBufferByteLength(typedArray); // TypeError: ArrayBuffer expected
// after
var len = arrayBufferByteLength(typedArray.buffer);
Defensive patterns

Strategy: validation

Validate before calling

function isArrayBuffer(v){ return Object.prototype.toString.call(v) === '[object ArrayBuffer]'; }
if (!isArrayBuffer(input)) throw new TypeError('expected an ArrayBuffer');

Type guard

function isArrayBuffer(v){ return Object.prototype.toString.call(v) === '[object ArrayBuffer]'; }

Try / catch

try { len = arrayBufferByteLength(input); } catch (e) { if (e instanceof TypeError) len = fallbackLength(input); else throw e; }

Prevention

When it happens

Trigger: Calling arrayBufferByteLength(O) with a non-ArrayBuffer value: a DataView, a typed array, a SharedArrayBuffer (in builds where the native getter path is skipped), a plain object, or undefined/null.

Common situations: Polyfilled environments (older engines without ArrayBuffer.prototype byteLength accessor) where the fallback path runs; code that confuses a typed array for its .buffer; passing a SharedArrayBuffer by mistake; transitive failure through other core-js polyfills (e.g. ArrayBuffer.prototype.slice, DataView internals) given a wrong argument.

Related errors


AI-assisted analysis of zloirock/core-js@84e45fba09 (2026-08-30). Data as JSON: /api/errors/bfa102b848a8442e. Report an issue: GitHub.