{"record":{"id":"ea2e41e344ff13e2","repo":"microsoft/TypeScript","slug":"private-accessor-was-defined-without-a-setter","errorCode":null,"errorMessage":"Private accessor was defined without a setter","messagePattern":"Private accessor was defined without a setter","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/compiler/factory/emitHelpers.ts","lineNumber":1351,"sourceCode":" * Writing to a private static set accessor (when defined, TS 4.3+):\r\n *      __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","sourceCodeStart":1333,"sourceCodeEnd":1369,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/src/compiler/factory/emitHelpers.ts#L1333-L1369","documentation":"Runtime TypeError from `__classPrivateFieldSet`. The helper throws when `kind === \"a\"` (accessor) and no setter function `f` was supplied — the private accessor is read-only (declared with only a `get`). Mirror of error 20, but on the write path.","triggerScenarios":"Assigning to a private accessor that has only a getter (`get #x() { ... }` with no `set #x`), compiled with the `__classPrivateFieldSet` helper (target below ES2022).","commonSituations":"Declaring a computed read-only private accessor and later trying to assign it; migrating readonly fields to accessors without adding setters; framework code that writes back to computed properties.","solutions":["Add a `set` to the private accessor if writes are intended.","Stop assigning to the read-only accessor at that call site.","Raise `target` to ES2022+ for native semantics and clearer errors.","Back the accessor with a writable private field and route writes through an explicit setter."],"exampleFix":"// before\nclass C {\n  get #ro() { return 42; }\n  set() { this.#ro = 1; }   // runtime TypeError\n}\n// after\nclass C {\n  #v = 42;\n  get #ro() { return this.#v; }\n  set #ro(x: number) { this.#v = x; }\n  set() { this.#ro = 1; }\n}","handlingStrategy":"validation","validationCode":"// Only assign to accessors you have explicitly declared a setter for;\n// keep the get/set pair together when authoring.","typeGuard":"type HasSetter<T> = T extends { readonly set: infer S } ? S : never;","tryCatchPattern":null,"preventionTips":["When adding a private getter that must stay read-only, audit the class for any writes to it.","Keep get/set pairs co-located so a missing setter is obvious in review."],"tags":["private-fields","runtime","downlevel","accessor","emit-helpers"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}