Description
Describe the bug
Svelte 5 produces an unhelpful error if:
- A reactive variable is assigned an object whose properties are referenced in markup
- The reactive variable gets reassigned to
undefined
Uncaught TypeError: get(...) is undefined
This obscures what caused the error. A more helpful error message would be, similar to WebKit's:
Uncaught TypeError: undefined is not an object (evaluating 'foo.bar')
It can be caused by a function that usually returns an object, but may possibly return undefined
, a common pattern in JS. This is the kind of bug that only happens in edge cases, making it hard to catch and reproduce.
In TypeScript, you do get a … is possibly 'undefined'
error in the editor when referencing the property, and using the optional chaining operator ?.
and nullish coalescing operator ??
prevents the error. Not all developers or libraries use TypeScript though, and one may have used type assertion, stopping TypeScript from detecting the type error.
In any case, informative error messages prevent headaches.
Related issue: #13619
(Thanks for your hard work on Svelte!)
Reproduction
Logs
Uncaught TypeError: get(...) is undefined
App playground:output:2870
update_reaction playground:output:1791
update_effect playground:output:1917
process_effects playground:output:2092
flush_queued_root_effects playground:output:1984
process_deferred playground:output:2031
schedule_effect playground:output:2048
mark_reactions playground:output:413
internal_set playground:output:334
set playground:output:320
on_click playground:output:2854
handle_event_propagation playground:output:2512
System Info
N/A
Severity
annoyance