swc-project/swc · error · TypeError

Symbol.asyncDispose is not defined.

Error message

Symbol.asyncDispose is not defined.

What it means

In the same `_ts_add_disposable_resource` helper, `await using` requires the well-known symbol `Symbol.asyncDispose`. The downlevel helper does not polyfill it; if the runtime lacks the symbol it throws `TypeError("Symbol.asyncDispose is not defined.")` before even reading the method off the value.

Source

Thrown at crates/swc_ecma_transforms_base/src/helpers/generated/_ts_add_disposable_resource.rs:18

// This file is generated by `cargo codegen helpers`. DO NOT MODIFY.

use super::{HelperDef, HelperName};

pub const DEF: HelperDef = HelperDef {
    name: HelperName::ts_add_disposable_resource,
    local: "_ts_add_disposable_resource",
    import_path: "@swc/helpers/_/_ts_add_disposable_resource",
    #[cfg(feature = "inline-helpers")]
    source: r#"function _ts_add_disposable_resource(env, value, async) {
    if (value !== null && value !== void 0) {
        if (typeof value !== "object" && typeof value !== "function") {
            throw new TypeError("Object expected.");
        }
        var dispose, inner;
        if (async) {
            if (!Symbol.asyncDispose) {
                throw new TypeError("Symbol.asyncDispose is not defined.");
            }
            dispose = value[Symbol.asyncDispose];
        }
        if (dispose === void 0) {
            if (!Symbol.dispose) {
                throw new TypeError("Symbol.dispose is not defined.");
            }
            dispose = value[Symbol.dispose];
            if (async) {
                inner = dispose;
            }
        }
        if (typeof dispose !== "function") {
            throw new TypeError("Object not disposable.");
        }
        if (inner) {
            dispose = function() {
                try {

View on GitHub (pinned to 5176682b65)

Solutions

  1. Polyfill before first use: `Symbol.asyncDispose ??= Symbol('Symbol.asyncDispose');` (and make resources expose the method under that same symbol).
  2. Use a `DisposableStack`/`SuppressedError` polyfill package that installs both symbols.
  3. Bump the runtime to Node >= 20.4 / a browser with Symbol.asyncDispose.
  4. Replace `await using` with explicit `try/finally` + `await res[Symbol.asyncDispose]?.()` on old runtimes.

Example fix

// before
// entry.mjs — Node 18
await using lock = acquireLock(); // TypeError: Symbol.asyncDispose is not defined.

// after
// entry.mjs — polyfill first
Symbol.asyncDispose ??= Symbol('Symbol.asyncDispose');
await using lock = acquireLock();
Defensive patterns

Strategy: fallback

Validate before calling

// Feature-detect and polyfill before any `await using` executes (entry point).
if (typeof Symbol.asyncDispose === 'undefined') {
  Symbol.asyncDispose = Symbol('Symbol.asyncDispose');
  // Resources must expose their method under this same symbol:
  // obj[Symbol.asyncDispose] ??= () => obj.close();
}
if (typeof SuppressedError === 'undefined') {
  globalThis.SuppressedError = class SuppressedError extends Error {};
}

Type guard

const supportsAsyncDispose = (): boolean => typeof (Symbol as { asyncDispose?: symbol }).asyncDispose === 'symbol';

Try / catch

try {
  await using res = open();
} catch (err) {
  if (err instanceof TypeError && /asyncDispose is not defined/.test(err.message)) {
    // runtime lacks the symbol — manual async cleanup instead
    const res = open();
    try { /* work */ } finally { await res.close(); }
  } else throw err;
}

Prevention

When it happens

Trigger: `await using x = res;` executed in an environment without `Symbol.asyncDispose` (Node < 20.4, older browsers) after SWC downleveled the declaration to the TS helper.

Common situations: Shipping TS 5.2+ `await using` to Node 18 LTS or Safari versions without explicit resource management; assuming the compiled helper defines the missing symbol; CI on modern Node but production on older LTS.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/6c0c386e3fef55bf. Report an issue: GitHub.