{"record":{"id":"6be7610c330c2d53","repo":"swc-project/swc","slug":"object-expected","errorCode":null,"errorMessage":"Object expected.","messagePattern":"Object expected\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_transforms_base/src/helpers/generated/_ts_add_disposable_resource.rs","lineNumber":13,"sourceCode":"// This file is generated by `cargo codegen helpers`. DO NOT MODIFY.\n\nuse super::{HelperDef, HelperName};\n\npub const DEF: HelperDef = HelperDef {\n    name: HelperName::ts_add_disposable_resource,\n    local: \"_ts_add_disposable_resource\",\n    import_path: \"@swc/helpers/_/_ts_add_disposable_resource\",\n    #[cfg(feature = \"inline-helpers\")]\n    source: r#\"function _ts_add_disposable_resource(env, value, async) {\n    if (value !== null && value !== void 0) {\n        if (typeof value !== \"object\" && typeof value !== \"function\") {\n            throw new TypeError(\"Object expected.\");\n        }\n        var dispose, inner;\n        if (async) {\n            if (!Symbol.asyncDispose) {\n                throw new TypeError(\"Symbol.asyncDispose is not defined.\");\n            }\n            dispose = value[Symbol.asyncDispose];\n        }\n        if (dispose === void 0) {\n            if (!Symbol.dispose) {\n                throw new TypeError(\"Symbol.dispose is not defined.\");\n            }\n            dispose = value[Symbol.dispose];\n            if (async) {\n                inner = dispose;\n            }\n        }\n        if (typeof dispose !== \"function\") {","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_transforms_base/src/helpers/generated/_ts_add_disposable_resource.rs#L1-L31","documentation":"`_ts_add_disposable_resource` backs TypeScript `using`/`await using` declarations that SWC downlevels for old targets. Explicit resource management only accepts objects, functions, `null`, and `undefined`; passing a primitive triggers `TypeError(\"Object expected.\")` because primitives cannot carry `[Symbol.dispose]`.","triggerScenarios":"`using x = 42;`, `using s = 'text';`, or an acquire function that conditionally returns a primitive, compiled with `using` declarations for a target without native support (Node < 20 / ES2022-).","commonSituations":"Trying to abuse `using` as a generic scope-exit hook for primitives; a `getHandle()` whose return type union includes primitives; TS 5.2+ code compiled for older runtimes.","solutions":["Only use `using`/`await using` with disposable resources — objects or functions exposing `[Symbol.dispose]`/`[Symbol.asyncDispose]` — plus `null`/`undefined`.","If you need cleanup around a primitive, wrap it: `using box = { [Symbol.dispose]: () => cleanup(), value };`.","Use plain `try/finally` for primitives instead of `using`."],"exampleFix":"// before\nfunction run() {\n  using handle = getHandle(); // returns 42 for some inputs\n  // TypeError: Object expected.\n}\n\n// after\nfunction run() {\n  const handle = getHandle();\n  if (typeof handle === 'number') { /* primitive path, no using */ return; }\n  using h = handle; // now provably an object\n}","handlingStrategy":"type-guard","validationCode":"// Mirror the helper's own rule before the `using` statement.\nconst isUsableResource = (v) =>\n  v === null || v === undefined || typeof v === 'object' || typeof v === 'function';\nconst res = acquire();\nif (!isUsableResource(res)) {\n  throw new TypeError(`using requires an object/function/null/undefined, got ${typeof res}`);\n}\nusing r = res;","typeGuard":"type UsableResource = Disposable | AsyncDisposable | null | undefined;\nconst isUsableResource = (v: unknown): v is UsableResource =>\n  v == null || typeof v === 'object' || typeof v === 'function';","tryCatchPattern":null,"preventionTips":["Type acquire functions as `Disposable | null | undefined` so primitives fail at compile time.","Reserve `using` for resources with real disposal semantics; use try/finally for scalar cleanup flags.","Wrap unavoidable primitive handles in a `{ [Symbol.dispose]: ... }` adapter at acquisition."],"tags":["using-declaration","explicit-resource-management","typescript","disposable"],"backgroundTag":"using-declaration-non-object","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}