yewstack/yew · error

I'm sure this event has a target!

Error message

I'm sure this event has a target!

What it means

This panic is the `.expect("I'm sure this event has a target!")` on `Event::target()` in Yew 0.23's wasm-bindgen guide (`handle_event`). `Event::target()` is an Option because events are not required to have a target — `Event::new` produces one with `target === null` until it is dispatched on a node. The guide uses expect for brevity before demonstrating `dyn_ref` casts; any targetless event reaching this function panics.

Source

Thrown at website/versioned_docs/version-0.23/concepts/basic-web-technologies/wasm-bindgen.mdx:146

`JsCast` provides both checked and unchecked methods of casting - so if at runtime if you are
unsure what type a certain object is, you can try to cast it, which returns possible failure types like
[`Option`](https://doc.rust-lang.org/std/option/enum.Option.html) and
[`Result`](https://doc.rust-lang.org/std/result/enum.Result.html).

A common example of this in [`web-sys`](./web-sys.mdx) is when you are trying to get the
target of an event. You might know what the target element is, but the
[`web_sys::Event`](https://wasm-bindgen.github.io/wasm-bindgen/api/web_sys/struct.Event.html) API will always return an [`Option<web_sys::EventTarget>`](https://wasm-bindgen.github.io/wasm-bindgen/api/web_sys/struct.Event.html#method.target).
You will need to cast it to the element type so you can call its methods.

```rust
// need to import the trait.
use wasm_bindgen::JsCast;
use web_sys::{Event, EventTarget, HtmlInputElement, HtmlSelectElement};

fn handle_event(event: Event) {
    let target: EventTarget = event
        .target()
        .expect("I'm sure this event has a target!");

    // maybe the target is a select element?
    if let Some(select_element) = target.dyn_ref::<HtmlSelectElement>() {
        // do something amazing here
        return;
    }

    // if it wasn't a select element then I KNOW it's a input element!
    let input_element: HtmlInputElement = target.unchecked_into();
}
```

The [`dyn_ref`](https://wasm-bindgen.github.io/wasm-bindgen/api/wasm_bindgen/trait.JsCast.html#method.dyn_ref)
method is a checked cast that returns an `Option<&T>`, which means the original type
can be used again if the cast failed and thus returned `None`. The
[`dyn_into`](https://wasm-bindgen.github.io/wasm-bindgen/api/wasm_bindgen/trait.JsCast.html#method.dyn_into)
method will consume `self`, as per convention for `into` methods in Rust, and the type returned is
`Result<T, Self>`. If the casting fails, the original `Self` value is returned in `Err`. You can try again

View on GitHub (pinned to 0e4a05472f)

Solutions

  1. Handle the Option: `let Some(target) = event.target() else { return; };`
  2. Keep the `dyn_ref::<HtmlSelectElement>()` / `dyn_ref::<HtmlInputElement>()` branching but under the unwrapped target
  3. Dispatch synthetic events from a concrete element when you control the test

Example fix

// before
let target: EventTarget = event
    .target()
    .expect("I'm sure this event has a target!");

// after
let Some(target: EventTarget) = event.target() else {
    return;
};
if let Some(select_element) = target.dyn_ref::<HtmlSelectElement>() {
    // do something amazing here
    return;
}
Defensive patterns

Strategy: type-guard

Validate before calling

fn handle_event(event: Event) {
    let Some(target) = event.target() else {
        return;
    };
    let _ = target;
}

Type guard

use wasm_bindgen::JsCast;
use web_sys::{EventTarget, HtmlSelectElement};

fn as_select(t: &EventTarget) -> Option<&HtmlSelectElement> {
    t.dyn_ref::<HtmlSelectElement>()
}

Prevention

When it happens

Trigger: Feeding `handle_event` a synthesized `Event` (built with `Event::new`, never dispatched); forwarding events from event-bus or test utilities that construct bare events; dispatch flows where the target has been cleared.

Common situations: Copy-pasting the casting example from wasm-bindgen.mdx into real listeners or unit tests; integrating with libraries that re-dispatch forwarded events.

Related errors


AI-assisted analysis of yewstack/yew@0e4a05472f (2026-08-22). Data as JSON: /api/errors/d4218c6def823f0d. Report an issue: GitHub.