You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<scriptlang="ts">letvalue: string|null=$state(null);letupper=$derived(value?.toUpperCase());// ...value is mutated somewhere else... </script>
This works at runtime, but TypeScript complains about value being of type never inside $derived.
I can see why it thinks that: value is not reassigned before line 3, therefore it's narrowed to type null, therefore the not-null case never happens.
However, with Svelte's $derived this expression will be run again when value is set to a non-null value, so this narrowing is incorrect.
There seems to be no mention of this in the documentation or tutorial. Some helpful people in the Discord suspect this is a bug.
The callback mechanism of $derived.by doesn't trigger these shenanigans:
Can someone please move that first issue already (or maybe close it in favor of this or the language tools one, which would at least be in the most relevant spot).
Describe the bug
Take the following (contrived) example:
This works at runtime, but TypeScript complains about
value
being of typenever
inside$derived
.I can see why it thinks that:
value
is not reassigned before line 3, therefore it's narrowed to typenull
, therefore the not-null casenever
happens.However, with Svelte's
$derived
this expression will be run again whenvalue
is set to a non-null value, so this narrowing is incorrect.There seems to be no mention of this in the documentation or tutorial. Some helpful people in the Discord suspect this is a bug.
The callback mechanism of
$derived.by
doesn't trigger these shenanigans:@CaptainCodeman also suggested this workaround which seems to trick TS into not narrowing down the type in mysterious ways:
Reproduction
Since the playground doesn't show TS errors: repro-svelte-14319
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: