{"record":{"id":"8db80021494ca2f9","repo":"oven-sh/bun","slug":"private-accessor-was-defined-without-a-getter","errorCode":null,"errorMessage":"Private accessor was defined without a getter","messagePattern":"Private accessor was defined without a getter","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"bench/snippets/private.mjs","lineNumber":9,"sourceCode":"import { bench, run } from \"../runner.mjs\";\n// This is a benchmark of the performance impact of using private properties.\n\nbench(\"Polyfillprivate\", () => {\n  \"use strict\";\n  var __classPrivateFieldGet =\n    (this && this.__classPrivateFieldGet) ||\n    function (receiver, state, kind, f) {\n      if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\n      if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver))\n        throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\n      return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\n    };\n  var __classPrivateFieldSet =\n    (this && this.__classPrivateFieldSet) ||\n    function (receiver, state, value, kind, f) {\n      if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\n      if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\n      if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver))\n        throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\n      return kind === \"a\" ? f.call(receiver, value) : f ? (f.value = value) : state.set(receiver, value), value;\n    };\n  var _Foo_state, _Foo_inc;\n  class Foo {\n    constructor() {\n      _Foo_state.set(this, 1);\n      _Foo_inc.set(this, 13);","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/oven-sh/bun/blob/8c5296ac459e8252d3cd702f3fbcbb0c249d95d5/bench/snippets/private.mjs#L1-L27","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add a getter to the private accessor so reads are legal","Expose reads through a regular public method or property on the class instead of reading the accessor directly","Raise the TypeScript compile target to ES2022+ so native #private syntax is emitted and the polyfill disappears"],"exampleFix":"// before (downleveled, set-only accessor)\nclass Foo {\n  set #x(v) { this._v = v; }\n  read() { return this.#x; } // TypeError: no getter\n}\n// after\nclass Foo {\n  get #x() { return this._v; }\n  set #x(v) { this._v = v; }\n  read() { return this.#x; }\n}","handlingStrategy":"type-guard","validationCode":"// before reading a downleveled private accessor, confirm a getter exists\nif (kind === 'a' && typeof f !== 'function') throw new TypeError('cannot read set-only private accessor');","typeGuard":"function hasPrivateGetter(kind, f) {\n  return kind !== 'a' || typeof f === 'function';\n}","tryCatchPattern":null,"preventionTips":["Compile with target ES2022 or higher so native #private accessors are used and misuse becomes a compile-time or early engine error","Declare getters for any private accessor you read, even if it returns a placeholder","Enable TypeScript's strict checks so accessor reads without getters are caught before downleveling"],"tags":["typescript","private-fields","private-accessors","downlevel-emit"],"backgroundTag":null,"analyzedSha":"8c5296ac459e8252d3cd702f3fbcbb0c249d95d5","analyzedAt":"2026-08-16T08:01:58.794Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}