Skip to content

Svelte 5 ($next) $derived compilation bug: Incorrect $.get() insertion for $state in filter, causing TypeError #16088

Closed
@Gianluca-9

Description

@Gianluca-9

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 $state variable. It appears the compiler incorrectly inserts $.get() calls, leading to TypeErrors at runtime.

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 $state string (search in src/routes/Warehouse.svelte), the compiled output seems to incorrectly access the search variable via $.get(search). This $.get() call does not return the expected string value, causing subsequent string methods like .toLowerCase() or array methods like .forEach() to fail with a TypeError.

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 $derived is enabled in Warehouse.svelte) outputs a function that incorrectly contains $.get(search):

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions