{"record":{"id":"5ab5e7e03589fa60","repo":"microsoft/TypeScript","slug":"cannot-use-in-operator-on-non-object","errorCode":null,"errorMessage":"Cannot use 'in' operator on non-object","messagePattern":"Cannot use 'in' operator on non-object","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/compiler/factory/emitHelpers.ts","lineNumber":1375,"sourceCode":"/**\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\n    importName: \"__classPrivateFieldIn\",\r\n    scoped: false,\r\n    text: `\r\n            var __classPrivateFieldIn = (this && this.__classPrivateFieldIn) || function(state, receiver) {\r\n                if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\r\n                return typeof state === \"function\" ? receiver === state : state.has(receiver);\r\n            };`,\r\n};\r\n\r\nconst addDisposableResourceHelper: UnscopedEmitHelper = {\r\n    name: \"typescript:addDisposableResource\",\r\n    importName: \"__addDisposableResource\",\r\n    scoped: false,\r\n    text: `\r\n        var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {\r\n            if (value !== null && value !== void 0) {\r\n                if (typeof value !== \"object\" && typeof value !== \"function\") throw new TypeError(\"Object expected.\");\r\n                var dispose, inner;\r\n                if (async) {\r\n                    if (!Symbol.asyncDispose) throw new TypeError(\"Symbol.asyncDispose is not defined.\");\r\n                    dispose = value[Symbol.asyncDispose];\r\n                }\r\n                if (dispose === void 0) {\r","sourceCodeStart":1357,"sourceCodeEnd":1393,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/src/compiler/factory/emitHelpers.ts#L1357-L1393","documentation":"Runtime TypeError from the `__classPrivateFieldIn` helper, which lowers `#field in expr`. Before the brand check it guards the receiver: if `receiver === null` or `typeof receiver` is neither `\"object\"` nor `\"function\"`, it throws. The native `in` operator has the same requirement, but this message comes from the downlevel helper.","triggerScenarios":"Evaluating `#x in value` where `value` is `null`, `undefined`, or a primitive (number/string/boolean/symbol/bigint), in code compiled with the `__classPrivateFieldIn` helper. Also reached by calling `__classPrivateFieldIn(state, primitive)` directly.","commonSituations":"User input or parsed values fed into a `#brand in obj` check without coercion; generic utility code that accepts `unknown`/`any` and runs a private-brand test; nullish short-circuits that were forgotten.","solutions":["Narrow the operand before the `in` check: `typeof o === \"object\" && o !== null && #x in o`.","Type the parameter as `object` (or the declaring class) so primitives are rejected statically.","Guard nullish values upstream so the `in` expression only ever sees objects.","Raise `target` to ES2022+ to get the native spec error."],"exampleFix":"// before\nfunction branded(o: any) {\n  return #x in o;          // throws if o is null/undefined/primitive\n}\n// after\nfunction branded(o: unknown) {\n  return typeof o === \"object\" && o !== null && #x in o;\n}","handlingStrategy":"validation","validationCode":"function isBranded(o: unknown): boolean {\n  return typeof o === \"object\" && o !== null && #x in o;\n}","typeGuard":"function isObjectLike(o: unknown): o is object { return typeof o === \"object\" && o !== null; }","tryCatchPattern":null,"preventionTips":["Always narrow `unknown`/`any` to `object` before any `#x in o` brand check.","Validate at the trust boundary so primitives never reach brand-check code."],"tags":["private-fields","runtime","downlevel","in-operator","emit-helpers"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}