{"record":{"id":"069a8d84a3433446","repo":"microsoft/TypeScript","slug":"cannot-write-private-member-to-an-object-whose-cla","errorCode":null,"errorMessage":"Cannot write private member to an object whose class did not declare it","messagePattern":"Cannot write private member to an object whose class did not declare it","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/compiler/factory/emitHelpers.ts","lineNumber":1352,"sourceCode":" *      __classPrivateFieldSet(<any>, <constructor>, <any>, \"a\", <function>)\r\n *\r\n * Writing to a private static set accessor (when not defined, TS 4.3+):\r\n *      __classPrivateFieldSet(<any>, <constructor>, <any>, \"a\", void 0)\r\n *      NOTE: This always results in a runtime error.\r\n *\r\n * Writing to a private static method (TS 4.3+):\r\n *      __classPrivateFieldSet(<any>, <constructor>, <any>, \"m\", <function>)\r\n *      NOTE: This always results in a runtime error.\r\n */\r\nconst classPrivateFieldSetHelper: UnscopedEmitHelper = {\r\n    name: \"typescript:classPrivateFieldSet\",\r\n    importName: \"__classPrivateFieldSet\",\r\n    scoped: false,\r\n    text: `\r\n            var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {\r\n                if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\r\n                if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\r\n                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\");\r\n                return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\r\n            };`,\r\n};\r\n\r\n/**\r\n * Parameters:\r\n *  @param state — One of the following:\r\n *      - A WeakMap when the member is a private instance field.\r\n *      - A WeakSet when the member is a private instance method or accessor.\r\n *      - A function value that should be the undecorated class constructor when the member is a private static field, method, or accessor.\r\n *  @param receiver — The object being checked if it has the private member.\r\n *\r\n * Usage:\r\n * This helper is used to transform `#field in expression` to\r\n *      `__classPrivateFieldIn(<weakMap/weakSet/constructor>, expression)`\r\n */\r\nconst classPrivateFieldInHelper: UnscopedEmitHelper = {\r\n    name: \"typescript:classPrivateFieldIn\",\r","sourceCodeStart":1334,"sourceCodeEnd":1370,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/src/compiler/factory/emitHelpers.ts#L1334-L1370","documentation":"Runtime TypeError from `__classPrivateFieldSet`, the write-path brand check. After the kind checks pass, the helper requires `state.has(receiver)` (instance) or `receiver === state` (static). If the receiver was not branded by the declaring class, the write is rejected. Mirror of error 21 on the write path.","triggerScenarios":"Assigning to a private member through a method invoked on an alien receiver (`C.prototype.setter.call(plainObj, v)`), or writing a static private member on something that is not the class constructor. Typically reached through `as any`, `Object.create`, or deserialized objects.","commonSituations":"Hydrating objects via `JSON.parse` and calling setters on them; proxy/subclass machinery that does not run the class constructor; cross-class helpers that accept `any`.","solutions":["Ensure the receiver is a genuine instance built by `new C()`.","Type setter parameters as the declaring class so alien receivers fail at compile time.","Rehydrate deserialized data into class instances before calling setters.","For static privates, invoke on the class constructor itself."],"exampleFix":"// before\nclass C {\n  #x = 0;\n  static set(o: any, v: number) { o.#x = v; }  // alien object throws\n}\nC.set(Object.create(C.prototype), 9);\n// after\nclass C {\n  #x = 0;\n  static set(o: C, v: number) { o.#x = v; }\n}\nconst c = new C();\nC.set(c, 9);","handlingStrategy":"validation","validationCode":"function safeSet(o: unknown, v: number) { if (o instanceof C) { (o as C).#x = v; } }","typeGuard":"function isC(o: unknown): o is C { return o instanceof C; }","tryCatchPattern":null,"preventionTips":["Never feed plain/deserialized objects into private-member setters.","Type all setter entry points as the declaring class, not `any`."],"tags":["private-fields","runtime","downlevel","brand-check","emit-helpers"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}