Description
Describe the bug
Hello Svelte Team,
I'm reporting a persistent compilation issue observed with Svelte 5's $derived rune when attempting to filter an array based on a
A minimal reproduction repository is available here: https://github.com/Gianluca-9/bug
When using $derived to filter an array (storeWarehouse.articles, defined as $state in src/lib/stores/warehouse.svelte.ts) based on a reactive
Reproduction
Steps to Reproduce:
Clone the repository:
Bash
git clone https://github.com/Gianluca-9/bug.git
cd bug
Install dependencies:
Bash
pnpm i
Start the development server:
Bash
pnpm dev
Open the application in your browser (usually http://localhost:5173/).
Observe the console for errors.
Relevant Code Causing the Issue (src/routes/Warehouse.svelte):
Expected Behavior:
The $derived variable wh should evaluate to an Article[] array, containing items filtered correctly based the search string. No TypeError should occur during evaluation or access. console.log($inspect(wh)) should show the internal representation, but its value property should be the filtered array.
Actual Behavior:
A TypeError is thrown at runtime, preventing the table from rendering correctly:
Uncaught TypeError: $.get(...).forEach is not a function (this error occurs when wh is accessed, e.g., in the {#each wh as art} loop or if a derived value depends on wh).
Furthermore, inspecting the wh variable using console.log($inspect(wh)) (when the
init () => {
return storeWarehouse.articles.filter((item) => item.descr.toLowerCase().includes($.get(search).toLowerCase()));
}
This demonstrates that the compiler is inserting the internal $.get macro unexpectedly, leading to runtime failures.
Workaround:
The filtering functionality is restored by changing wh from a $derived to a $state variable and updating it imperatively within an $effect block that reacts to changes in storeWarehouse.articles and search (as shown in the commented-out code in Warehouse.svelte).
Thank you for your time and assistance in investigating this compiler bug.
Logs
System Info
Svelte: 5.0.0 (Questa è la versione stabile GA! Non più next.1!)
@sveltejs/kit: 2.16.0
@sveltejs/vite-plugin-svelte: 5.0.0
Vite: 6.2.6
TypeScript: 5.0.0 (o più recente, dato che indica ^5.0.0)
Node.js: 22.16.0
Severity
annoyance