{"record":{"id":"51151a1d1c8f8565","repo":"oven-sh/bun","slug":"cannot-read-private-member-from-an-object-whose-cl","errorCode":null,"errorMessage":"Cannot read private member from an object whose class did not declare it","messagePattern":"Cannot read private member from an object whose class did not declare it","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"bench/snippets/private.mjs","lineNumber":11,"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);\n    }\n    run() {","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/oven-sh/bun/blob/8c5296ac459e8252d3cd702f3fbcbb0c249d95d5/bench/snippets/private.mjs#L1-L29","documentation":"The WeakMap-membership guard in the TypeScript __classPrivateFieldGet polyfill: for a field-backed private (state is a WeakMap), the receiver must already be registered (state.has(receiver)) — which happens only when the declaring class's constructor/field initializer ran for that object. Reading the private member from any object that did not initialize it throws this TypeError. It is the downlevel equivalent of native `#field` access on a non-instance, and in private.mjs it sits inside the polyfill used by the 'Polyfillprivate' bench.","triggerScenarios":"Calling a class method with a borrowed receiver, e.g. Foo.prototype.run.call(notAFoo), so `this` lacks an entry in the field's WeakMap; accessing a downleveled private field on a plain object; an instance created without running the declaring constructor.","commonSituations":"Detached methods / destructured-then-called class methods losing `this`; prototype borrowing; mixin patterns that copy methods onto unrelated objects; mocks in tests replacing instances.","solutions":["Invoke methods on real instances of the declaring class (or bind them in the constructor: this.run = this.run.bind(this))","Guard borrowed calls with an instanceof check before touching privates","Prefer ES2022+ native #fields where possible — same error class, but surfaced directly by the engine"],"exampleFix":"// before\nFoo.prototype.run.call(otherObj); // TypeError: otherObj never declared the field\n// after\nif (otherObj instanceof Foo) otherObj.run();","handlingStrategy":"type-guard","validationCode":"// guard borrowed receivers before touching private members\nif (!(this instanceof Foo)) throw new TypeError('method must be called on a Foo instance');","typeGuard":"function isFooInstance(receiver) {\n  return receiver instanceof Foo;\n}","tryCatchPattern":null,"preventionTips":["Bind class methods in the constructor or use arrow-function fields when methods are detached, so `this` stays a real instance","Add instanceof guards at the top of methods that borrow receivers (event handlers, callbacks)","Never call Foo.prototype.method.call(x) on non-instances; with native #fields the engine throws the same class of TypeError"],"tags":["typescript","private-fields","weakmap","receiver","instanceof"],"backgroundTag":null,"analyzedSha":"8c5296ac459e8252d3cd702f3fbcbb0c249d95d5","analyzedAt":"2026-08-16T08:01:58.794Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}