-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Closed
Labels
Description
Describe the bug
State declarations are transformed thusly:
-let count = $state(0);
+let count = $.source(0);If we tried to console.log(count) before the declaration, we would get a TDZ error:
Can't access lexical declaration 'count' before initialization
But if it's a var instead, we don't — instead we try to get(undefined).
-console.log(count);
+console.log($.get(count));This errors with
signal is undefined
which is cryptic and unhelpful.
Possible remedies:
- add a
if (signal === undefined) return signalto the top ofgetsvelte/packages/svelte/src/internal/client/runtime.js
Lines 722 to 732 in 5094cb9
/** * @template V * @param {Value<V>} signal * @returns {V} */ export function get(signal) { var flags = signal.f; if ((flags & DESTROYED) !== 0) { return signal.v; } - disallow
var - hoist the declaration
Option 1 means doing a tiny bit of extra work on every get for something that's a real edge case. Option 2 feels like a bit of a cop-out. So I think we should probably do option 3.
Reproduction
Logs
No response
System Info
nextSeverity
annoyance