{"record":{"id":"48bd9c4e9ff87bc6","repo":"microsoft/TypeScript","slug":"object-expected-48bd9c","errorCode":null,"errorMessage":"Object expected.","messagePattern":"Object expected\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/compiler/factory/emitHelpers.ts","lineNumber":1387,"sourceCode":"const 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\n                    if (!Symbol.dispose) throw new TypeError(\"Symbol.dispose is not defined.\");\r\n                    dispose = value[Symbol.dispose];\r\n                    if (async) inner = dispose;\r\n                }\r\n                if (typeof dispose !== \"function\") throw new TypeError(\"Object not disposable.\");\r\n                if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };\r\n                env.stack.push({ value: value, dispose: dispose, async: async });\r\n            }\r\n            else if (async) {\r\n                env.stack.push({ async: true });\r\n            }\r\n            return value;\r","sourceCodeStart":1369,"sourceCodeEnd":1405,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/src/compiler/factory/emitHelpers.ts#L1369-L1405","documentation":"Runtime TypeError from the `__addDisposableResource` helper, emitted for the Explicit Resource Management feature (`using`/`await using`) when targeting runtimes that lack native support. The helper throws when `value` is neither `null`/`undefined` nor of type `\"object\"`/`\"function\"` — i.e. a primitive was bound with `using`.","triggerScenarios":"Writing `using x = <primitive>` (number, string, boolean, symbol, bigint) where the downlevel helper runs (target without native `using`). Also reached by manual `__addDisposableResource(env, primitive, async)` calls.","commonSituations":"Binding a configuration value or numeric handle with `using` instead of an object; refactors that assign a primitive to a `using` variable; envs where `using` is transpiled because the target lacks the feature.","solutions":["Bind only objects (or null/undefined to skip) with `using`.","Wrap the primitive in a small disposable object (`{ [Symbol.dispose]() {} }`).","Set `target`/`lib` high enough that `using` is emitted natively and the helper is not generated.","Type the `using` variable as a disposable so primitives are rejected at compile time."],"exampleFix":"// before\nusing handle = 42;             // primitive -> runtime TypeError\n// after\nusing handle = { [Symbol.dispose]() { /* close */ } } as Disposable;","handlingStrategy":"validation","validationCode":"// Validate before binding (when types were bypassed):\nfunction asDisposable<T>(v: T): T | null {\n  if (v === null || v === undefined) return null;\n  if (typeof v !== \"object\" && typeof v !== \"function\") throw new TypeError(\"Object expected\");\n  return v;\n}","typeGuard":"function isDisposable(v: unknown): v is Disposable { return v !== null && v !== undefined && typeof (v as any)[Symbol.dispose] === \"function\"; }","tryCatchPattern":null,"preventionTips":["Never bind primitives to `using`/`await using`.","Use `lib: [\"disposable\"]` / high `target` so native semantics apply.","Type `using` bindings as `Disposable`/`AsyncDisposable` to catch mistakes statically."],"tags":["disposable","using","runtime","downlevel","emit-helpers"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}