{"record":{"id":"c4880bea2466ca1e","repo":"swc-project/swc","slug":"symbol-dispose-is-not-defined","errorCode":null,"errorMessage":"Symbol.dispose is not defined.","messagePattern":"Symbol\\.dispose is not defined\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_transforms_base/src/helpers/generated/_ts_add_disposable_resource.rs","lineNumber":24,"sourceCode":"    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\") {\n            throw new TypeError(\"Object not disposable.\");\n        }\n        if (inner) {\n            dispose = function() {\n                try {\n                    inner.call(this);\n                } catch (e) {\n                    return Promise.reject(e);\n                }\n            };\n        }","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_transforms_base/src/helpers/generated/_ts_add_disposable_resource.rs#L6-L42","documentation":"The sync path of `_ts_add_disposable_resource` requires `Symbol.dispose`. When the downleveled `using` declaration runs in a runtime without that well-known symbol, the helper throws `TypeError(\"Symbol.dispose is not defined.\")` instead of silently skipping disposal.","triggerScenarios":"`using x = res;` executed where `Symbol.dispose` is absent (Node < 20, ES2022-era browsers) with output compiled to the TS helper.","commonSituations":"Same as the async case: modern TS features deployed to older LTS runtimes; edge/serverless runtimes pinned to old V8; assuming the helper brings its own symbol.","solutions":["Polyfill once at startup: `Symbol.dispose ??= Symbol('Symbol.dispose');` and ensure resources define `[Symbol.dispose]` under that key.","Use a polyfill package for explicit resource management that installs Symbol.dispose and DisposableStack.","Upgrade the runtime to one shipping Symbol.dispose.","Fall back to `try/finally` with manual dispose calls on legacy runtimes."],"exampleFix":"// before\nusing conn = openConnection(); // Node 18: TypeError: Symbol.dispose is not defined.\n\n// after\nSymbol.dispose ??= Symbol('Symbol.dispose');\nusing conn = openConnection();","handlingStrategy":"fallback","validationCode":"// Feature-detect and polyfill Symbol.dispose at the entry point.\nif (typeof Symbol.dispose === 'undefined') {\n  Symbol.dispose = Symbol('Symbol.dispose');\n  // Attach resources under the same key: res[Symbol.dispose] ??= () => res.close();\n}","typeGuard":"const supportsDispose = (): boolean => typeof (Symbol as { dispose?: symbol }).dispose === 'symbol';","tryCatchPattern":"try {\n  using res = open();\n} catch (err) {\n  if (err instanceof TypeError && /Symbol.dispose is not defined/.test(err.message)) {\n    const res = open();\n    try { /* work */ } finally { res.close(); } // manual disposal fallback\n  } else throw err;\n}","preventionTips":["Install a `DisposableStack`/explicit-resource-management polyfill package as the first import in the bundle.","Ensure polyfilled resources and the `using` site share the same symbol instance (same polyfill, same realm).","Prefer raising the minimum runtime over shipping long-lived polyfills."],"tags":["using-declaration","explicit-resource-management","polyfill","symbol-dispose"],"backgroundTag":"symbol-dispose-undefined","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}