{"record":{"id":"111fe478c1057c15","repo":"swc-project/swc","slug":"using-declarations-can-only-be-used-with-objects-111fe4","errorCode":null,"errorMessage":"using declarations can only be used with objects, functions, null, or undefined.","messagePattern":"using declarations can only be used with objects, functions, null, or undefined\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_transforms_base/src/helpers/generated/_using_ctx.rs","lineNumber":26,"sourceCode":"    import_path: \"@swc/helpers/_/_using_ctx\",\n    #[cfg(feature = \"inline-helpers\")]\n    source: r#\"function _using_ctx() {\n    var _disposeSuppressedError = typeof SuppressedError === \"function\"\n            // eslint-disable-next-line no-undef\n            ? SuppressedError\n            : (function(error, suppressed) {\n                var err = new Error();\n                err.name = \"SuppressedError\";\n                err.suppressed = suppressed;\n                err.error = error;\n                return err;\n            }),\n        empty = {},\n        stack = [];\n    function using(isAwait, value) {\n        if (value != null) {\n            if (Object(value) !== value) {\n                throw new TypeError(\"using declarations can only be used with objects, functions, null, or undefined.\");\n            }\n            // core-js-pure uses Symbol.for for polyfilling well-known symbols\n            if (isAwait) {\n                var dispose = value[Symbol.asyncDispose || Symbol.for(\"Symbol.asyncDispose\")];\n            }\n            if (dispose == null) {\n                dispose = value[Symbol.dispose || Symbol.for(\"Symbol.dispose\")];\n            }\n            if (typeof dispose !== \"function\") {\n                throw new TypeError(`Property [Symbol.dispose] is not a function.`);\n            }\n            stack.push({ v: value, d: dispose, a: isAwait });\n        } else if (isAwait) {\n            // provide the nullish `value` as `d` for minification gain\n            stack.push({ d: value, a: isAwait });\n        }\n        return value;\n    }","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_transforms_base/src/helpers/generated/_using_ctx.rs#L8-L44","documentation":"`_using_ctx` bundles a whole scope's `using`/`await using` declarations into a stack with error handling (including a `SuppressedError` fallback). Its inner `using(isAwait, value)` function performs the same spec check as `_using`: primitives are rejected with `TypeError: using declarations can only be used with objects, functions, null, or undefined.`","triggerScenarios":"Any `using` declaration inside a scope that also needs the error/suppression context (multiple resources, try/catch around the block) compiled to `_using_ctx`, where one of the resources evaluates to a primitive (number, string, boolean, symbol, bigint).","commonSituations":"Mixing several resources in one block where one acquire path returns a primitive fallback (e.g. `using lock = tryAcquire() || 0;`); adopting explicit resource management in bundler output that uses the ctx helper shape.","solutions":["Ensure every resource bound by `using`/`await using` in the block is an object/function or explicitly `null`/`undefined`.","Wrap primitive fallbacks in a disposable adapter object.","Split the primitive resource out of the `using` block into plain `try/finally` handling.","Type the acquire functions' returns as `Disposable | null | undefined` so the compiler catches primitives."],"exampleFix":"// before\n{\n  using a = getRes();\n  using b = tryLock() || 0; // primitive slips in\n  // TypeError: using declarations can only be used with objects...\n}\n\n// after\n{\n  using a = getRes();\n  const raw = tryLock();\n  using b = raw ?? { [Symbol.dispose]: () => {} };\n}","handlingStrategy":"type-guard","validationCode":"// Validate every resource in the block before entering it (same check as the ctx helper).\nconst ok = resources.every(\n  (r) => r === null || r === undefined || Object(r) === r\n);\nif (!ok) throw new TypeError('all using resources must be objects/functions/null/undefined');","typeGuard":"const isUsingCompatible = (v: unknown): v is object | null | undefined =>\n  v == null || (typeof v === 'object' || typeof v === 'function');","tryCatchPattern":null,"preventionTips":["Keep `using` blocks homogeneous: every bound resource must be disposable-or-nullish by construction.","Push primitive values out of `using` blocks into plain variables handled by try/finally.","Add compile-time types (`Disposable | null`) to resource providers so primitives cannot flow in."],"tags":["using-declaration","using-ctx","explicit-resource-management","typeerror"],"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"}