oven-sh/bun · error · TypeError

Private accessor was defined without a getter

Error message

Private accessor was defined without a getter

What it means

This is the TypeScript downlevel-emit polyfill guard in __classPrivateFieldGet: when kind === 'a' (accessor) and no getter function `f` was passed, reading the private accessor throws this TypeError. It is the pre-ES2022 equivalent of reading `this.#x` on a `set`-only private accessor — the field exists but has no getter, so the read is illegal. In private.mjs the helper is merely defined inside the 'Polyfillprivate' bench; the class under test only uses kind 'f' fields, so a throw means the helper was invoked with an accessor kind and a missing getter.

Source

Thrown at bench/snippets/private.mjs:9

import { bench, run } from "../runner.mjs";
// This is a benchmark of the performance impact of using private properties.

bench("Polyfillprivate", () => {
  "use strict";
  var __classPrivateFieldGet =
    (this && this.__classPrivateFieldGet) ||
    function (receiver, state, kind, f) {
      if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
      if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
        throw new TypeError("Cannot read private member from an object whose class did not declare it");
      return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
    };
  var __classPrivateFieldSet =
    (this && this.__classPrivateFieldSet) ||
    function (receiver, state, value, kind, f) {
      if (kind === "m") throw new TypeError("Private method is not writable");
      if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
      if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
        throw new TypeError("Cannot write private member to an object whose class did not declare it");
      return kind === "a" ? f.call(receiver, value) : f ? (f.value = value) : state.set(receiver, value), value;
    };
  var _Foo_state, _Foo_inc;
  class Foo {
    constructor() {
      _Foo_state.set(this, 1);
      _Foo_inc.set(this, 13);

View on GitHub (pinned to 8c5296ac45)

Solutions

  1. Add a getter to the private accessor so reads are legal
  2. Expose reads through a regular public method or property on the class instead of reading the accessor directly
  3. Raise the TypeScript compile target to ES2022+ so native #private syntax is emitted and the polyfill disappears

Example fix

// before (downleveled, set-only accessor)
class Foo {
  set #x(v) { this._v = v; }
  read() { return this.#x; } // TypeError: no getter
}
// after
class Foo {
  get #x() { return this._v; }
  set #x(v) { this._v = v; }
  read() { return this.#x; }
}
Defensive patterns

Strategy: type-guard

Validate before calling

// before reading a downleveled private accessor, confirm a getter exists
if (kind === 'a' && typeof f !== 'function') throw new TypeError('cannot read set-only private accessor');

Type guard

function hasPrivateGetter(kind, f) {
  return kind !== 'a' || typeof f === 'function';
}

Prevention

When it happens

Trigger: Downleveled TypeScript (target below ES2022 / older emit) where generated __classPrivateFieldGet(this, _x, 'a') runs against a private accessor that declared only a setter; or hand-calling the polyfill with kind 'a' and an undefined getter argument.

Common situations: Libraries compiled with an old target shipping downleveled #private accessors; consuming such a library where a set-only accessor is read; mixing polyfill and native class versions of the same class.

Related errors


AI-assisted analysis of oven-sh/bun@8c5296ac45 (2026-08-16). Data as JSON: /api/errors/8db80021494ca2f9. Report an issue: GitHub.