swc-project/swc · error

illegal conversion: Cannot convert {:?} to IdOrRest

Error message

illegal conversion: Cannot convert {:?} to IdOrRest

What it means

This panic fires in the `From<TsFnParamOutput> for IdOrRest` conversion when a converted TypeScript function parameter is neither a plain identifier nor a rest element (i.e. it is an Array or Object destructuring pattern). The conversion only supports Id and Rest variants because the target type IdOrRest has no representation for destructuring patterns, so any other variant is rejected as an illegal conversion.

Source

Thrown at crates/swc_estree_compat/src/babelify/typescript.rs:99

    fn babelify(self, ctx: &Context) -> Self::Output {
        match self {
            TsFnParam::Ident(i) => TsFnParamOutput::Id(i.babelify(ctx)),
            TsFnParam::Array(a) => TsFnParamOutput::Array(a.babelify(ctx)),
            TsFnParam::Rest(r) => TsFnParamOutput::Rest(r.babelify(ctx)),
            TsFnParam::Object(o) => TsFnParamOutput::Object(o.babelify(ctx)),
            #[cfg(swc_ast_unknown)]
            _ => panic!("unable to access unknown nodes"),
        }
    }
}

impl From<TsFnParamOutput> for IdOrRest {
    fn from(o: TsFnParamOutput) -> Self {
        match o {
            TsFnParamOutput::Id(i) => IdOrRest::Id(i),
            TsFnParamOutput::Rest(r) => IdOrRest::Rest(r),
            _ => panic!("illegal conversion: Cannot convert {:?} to IdOrRest", &o),
        }
    }
}

impl From<TsFnParamOutput> for Identifier {
    fn from(o: TsFnParamOutput) -> Self {
        match o {
            TsFnParamOutput::Id(i) => i,
            _ => panic!("illegal conversion: Cannot convert {:?} to Identifier", &o),
        }
    }
}

impl Babelify for TsTypeParamDecl {
    type Output = TSTypeParameterDeclaration;

    fn babelify(self, ctx: &Context) -> Self::Output {
        TSTypeParameterDeclaration {

View on GitHub (pinned to 5176682b65)

Solutions

  1. Ensure the source TypeScript function parameter is a simple identifier or a rest parameter before converting to IdOrRest
  2. If a destructuring parameter is possible at the call site, match on TsFnParamOutput::Array/Object first and reject or transform them instead of relying on the blanket From impl
  3. Pre-check the TsFnParam variant before calling .into() and surface a descriptive error to the caller instead of panicking
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at crates/swc_estree_compat/src/babelify/typescript.rs:99 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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