leptos-rs/leptos · error

incorrect element type

Error message

incorrect element type

What it means

After obtaining the event target Element, event_target casts it to the requested type T via T::cast_from and panics with 'incorrect element type' when the target isn't of type T. The library throws it because the caller statically requested a specific element type the runtime event didn't produce.

Solutions

  1. Verify the requested type matches the element the listener is attached to (and delegation assumptions)
  2. Check ev.target() manually and cast with T::cast_from, handling None instead of panicking
  3. Prefer ev.current_target()/current_target_element when you want the element the listener was registered on
  4. Scope delegated handlers so only expected child element types can trigger them

Example fix

// before
let btn: HtmlButtonElement = renderer.event_target(&ev); // panics if target is <svg>
// after
let t = ev.current_target().expect("listener element");
let btn = HtmlButtonElement::cast_from(t.unchecked_into());
if btn.is_none() { return; } // ignore unexpected targets
Defensive patterns

Strategy: type-guard

Type guard

fn target_matches<T: CastFrom<Element>>(ev: &web_sys::Event) -> Option<T> {
    ev.target().and_then(|t| T::cast_from(t.unchecked_into::<Element>()))
}

Try / catch

// use the guard instead of the panicking helper:
if let Some(el) = target_matches::<MyEl>(&ev) { /* handle */ }
// else: ignore or handle unexpected target type

Prevention

When it happens

Trigger: Calling event_target::<T> with a T that doesn't match the actual target, e.g. handler on a parent (delegated listener) where the clicked child is a different tag; button handler receiving clicks on an embedded <svg> or <img>; using event_target inside a document-level listener.

Common situations: Event delegation where bubbling targets differ from the listened element; mixing typed handlers with delegated events; clicks landing on child nodes (text/icons) whose element type differs from the expected one.

Related errors


AI-assisted analysis of leptos-rs/leptos@32d20f6c9d (2026-09-01). Data as JSON: /api/errors/3575f8e4ec86e20b. Report an issue: GitHub.

Appendix: source

Thrown at tachys/src/renderer/dom.rs:364

                    ),
                    &el,
                    "removeEventListener"
                )
            });
            move || cb()
        })
    }

    pub fn event_target<T>(ev: &Event) -> T
    where
        T: CastFrom<Element>,
    {
        let el = ev
            .unchecked_ref::<web_sys::Event>()
            .target()
            .expect("event.target not found")
            .unchecked_into::<Element>();
        T::cast_from(el).expect("incorrect element type")
    }

    pub fn add_event_listener_delegated(
        el: &Element,
        name: Cow<'static, str>,
        delegation_key: Cow<'static, str>,
        cb: Box<dyn FnMut(Event)>,
    ) -> RemoveEventHandler<Element> {
        let cb = Closure::wrap(cb);
        let key = intern(&delegation_key);
        or_debug!(
            js_sys::Reflect::set(el, &JsValue::from_str(key), cb.as_ref()),
            el,
            "set property"
        );

        GLOBAL_EVENTS.with_borrow_mut(|events| {
            if !events.contains(&name) {

View on GitHub (pinned to 32d20f6c9d)