swc-project/swc · error · TypeError

Duplicated element (${elements[j].key})

Error message

Duplicated element (${elements[j].key})

What it means

The typescript wasm binding (used by @swc/wasm-typescript) funnels every operation through try_with_json_handler, which collects diagnostics via a Handler and expects the operation to succeed whenever no errors were emitted. Ok(ret.expect("it should not fail without emitting errors to handler")) panics when the underlying operation returns Err without reporting anything to the handler - an internal contract violation, since well-formed failures are supposed to arrive as JSON diagnostics.

Source

Thrown at crates/swc_ecma_transforms_base/src/helpers/generated/_decorate.rs:165

            for (var j = 0; j < newExtras.length; j++) _addElementPlacement(newExtras[j], placements);
            extras.push.apply(extras, newExtras);
        }
    }

    return { element: element, finishers: finishers, extras: extras };
}
function _decorateConstructor(elements, decorators) {
    var finishers = [];
    for (var i = decorators.length - 1; i >= 0; i--) {
        var obj = _fromClassDescriptor(elements);
        var elementsAndFinisher = _toClassDescriptor((0, decorators[i])(obj) || obj);
        if (elementsAndFinisher.finisher !== undefined) finishers.push(elementsAndFinisher.finisher);
        if (elementsAndFinisher.elements !== undefined) {
            elements = elementsAndFinisher.elements;
            for (var j = 0; j < elements.length - 1; j++) {
                for (var k = j + 1; k < elements.length; k++) {
                    if (elements[j].key === elements[k].key && elements[j].placement === elements[k].placement) {
                        throw new TypeError("Duplicated element (" + elements[j].key + ")");
                    }
                }
            }
        }
    }

    return { elements: elements, finishers: finishers };
}
function _fromElementDescriptor(element) {
    var obj = { kind: element.kind, key: element.key, placement: element.placement, descriptor: element.descriptor };
    var desc = { value: "Descriptor", configurable: true };
    Object.defineProperty(obj, Symbol.toStringTag, desc);
    if (element.kind === "field") obj.initializer = element.initializer;

    return obj;
}
function _toElementDescriptors(elementObjects) {
    if (elementObjects === undefined) return;

View on GitHub (pinned to 5176682b65)

Solutions

  1. Capture the JS exception (the wasm panic crosses the boundary as a rejected Error) and extract the input that caused it; verify it parses with the same binding version in Node to isolate runtime factors.
  2. Upgrade @swc/wasm-typescript (and any @swc/core wrappers) to the latest patch - handler-emission gaps are fixed as bugs.
  3. File an swc issue with the minimal TypeScript input and binding version.
  4. Guard deployments with a fallback transformer for inputs that trigger the panic.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const out = stripTypes(source, opts);
} catch (e) {
  const msg = String(e?.message ?? e);
  if (msg.includes('it should not fail without emitting errors to handler')) {
    // internal invariant panic: no diagnostics were produced
    reportToTracker({ kind: 'swc-wasm-invariant', source, version });
    return fallbackStrip(source);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling the binding's transform/strip entry points with an input that drives swc_ts_fast_strip (or the wrapped operation) down an Err path that never emits to the handler - e.g. an internal bail on unsupported syntax constructs or resource exhaustion inside the wasm module.

Common situations: Using @swc/wasm-typescript in browser-based playground/build tools and feeding unusual TypeScript (decorators + satisfy chains, extremely deep nesting) that trips an unlogged failure; version skew between the wasm binding and the rust core; memory pressure in wasm runtimes turning an allocation failure into a plain Err.

Related errors


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