yewstack/yew · warning
I'm sure this event has a target!
Error message
I'm sure this event has a target!
What it means
This docs example calls event.target() and expects a value. Event.target() returns Option: it is None for events that were never dispatched, synthetic events created and fired manually without a target, and some framework-generated events. When that happens the expect panics - the snippet is only safe for genuine browser-dispatched DOM events.
Source
Thrown at website/versioned_docs/version-0.21/concepts/basic-web-technologies/wasm-bindgen.mdx:147
`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 againView on GitHub (pinned to 0e4a05472f)
Solutions
- Prefer current_target() when you want the element the listener is attached to - it is always set during dispatch
- Handle the Option: if let Some(target) = event.target() { ... }
- When dispatching synthetic events, dispatch them on a real node so the target is populated
- Use dyn_ref instead of dyn_into for downstream casts so mismatches degrade gracefully instead of panicking
Example fix
// before
let target: EventTarget = event.target().expect("I'm sure this event has a target!");
// after
if let Some(target) = event.target() {
// inspect target...
}
// or, when you actually want the element the listener is bound to:
if let Some(current) = event.current_target() {
// ...
} Defensive patterns
Strategy: type-guard
Type guard
fn target_as_element(event: &web_sys::Event) -> Option<web_sys::Element> {
event.target().and_then(|t| t.dyn_into::<web_sys::Element>().ok())
} Prevention
- Prefer current_target() when the listener's own element is what you want
- Never expect() on event.target(); handle the Option
- Dispatch synthetic events from a real node so the target is populated
When it happens
Trigger: Using the copied handler for programmatically created events (Event::new("custom") inspected before/without a real dispatch); test utilities dispatching bare events; handlers for event types whose target can legitimately be null.
Common situations: Reusing the doc handler for custom application events dispatched via dispatchEvent; event objects inspected outside handlers; testing harnesses that construct Event values directly.
Related errors
- mouse event doesn't have a target
- event target should be of type HtmlElement
- global document not set
- I'm sure this event has a target!
- mouse event doesn't have a target
AI-assisted analysis of yewstack/yew@0e4a05472f (2026-08-22).
Data as JSON: /api/errors/022f3ed3a55ce167.
Report an issue: GitHub.