oven-sh/bun · error · TypeError

Private accessor was defined without a setter

Error message

Private accessor was defined without a setter

What it means

The setter guard in the TypeScript __classPrivateFieldSet polyfill: when kind === 'a' (accessor) and no setter function `f` was passed, writing the private accessor throws this TypeError. It is the downlevel equivalent of assigning to a getter-only private accessor (`this.#x = value` where only `get #x()` is declared) — the field exists but writes are illegal, matching the native strict-mode TypeError.

Source

Thrown at bench/snippets/private.mjs:18

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);
    }
    run() {
      let n = 1000000;
      while (n-- > 0) {
        __classPrivateFieldSet(
          this,
          _Foo_state,
          __classPrivateFieldGet(this, _Foo_state, "f") + __classPrivateFieldGet(this, _Foo_inc, "f"),
          "f",

View on GitHub (pinned to 8c5296ac45)

Solutions

  1. Add a `set #x(value)` alongside the getter so writes are legal
  2. Route mutations through a method on the class that updates the underlying private field the getter reads from
  3. If the value must truly be read-only, remove the write site — that is the intended design

Example fix

// before
class Foo {
  get #x() { return this._x; }
  set(v) { this.#x = v; } // TypeError: no setter
}
// after
class Foo {
  get #x() { return this._x; }
  set #x(v) { this._x = v; }
  set(v) { this.#x = v; }
}
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: Downleveled TypeScript executing `this.#x = v` against a private accessor that declared only a getter (generated __classPrivateFieldSet with kind 'a' and no setter); or hand-calling the polyfill that way.

Common situations: Computed/read-only private state (caching lazy values) that later code tries to overwrite; refactoring a read-only accessor without updating all writers; libraries compiled to old targets where the compiler cannot catch cross-file writes.

Related errors


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