-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Closed as not planned
Description
Describe the bug
When there is a state parameter in a function declaration and the state is not being used inside the function, the signal gets compiled to get() instead of get(x). The function fails to execute and throws an undefined type error in the browser console.
This seems to only apply to nested functions and on npm run build (works correctly on local dev). The error can also surface if the state parameter is conditionally used inside the function.
Error thrown in the browser console:
Blc3Cr0p.js:95 Uncaught TypeError: Cannot read properties of undefined (reading 'f')
at get (DJF-AMb5.js:1268:22)
at func1 (2.DzV7yYn4.js:21:13)
at HTMLButtonElement.on_click_1 (2.DzV7yYn4.js:8:34)
at HTMLDivElement.handle_event_propagation (Blc3Cr0p.js:72:16)
Workarounds:
- if parameter is unused, remove the unused parameter from the function declaration
- if parameter is used inside a conditional, access it somewhere up the chain so that it is used earlier
e.g.
// this will compile incorrectly
const func = (a, d) => {
if (d) {
a++;
console.log(d);
}
};
// this will work
const func = (a, d) => {
if (a && d) {
a++;
console.log(d);
}
};
Reproduction
repo: https://github.com/fong/svelte-get-signal-undefined
npm run buildand thennpm run previewwill throw errornpm run devwill work correctly
example build: https://svelte-get-signal-undefined.pages.dev/
source code:
let a = $state(0);
const func1 = () => {
let d = a * a;
if (d <= 4) {
func2(a, d);
} else {
func3(a, d);
}
};
// compiles to "func2(get(a), d);" - correct
const func2 = (a, d) => {
if (a && d) {
console.log("func2", d);
}
};
// compiles to "func3(get(), d);" - incorrect
const func3 = (a, d) => {
console.log("func3", d);
};
compiles to:
let a = state(0);
const func1 = () => {
let d = get(a) * get(a);
if (d <= 4) {
func2(get(a), d);
} else {
func3(get(), d); <--- this is throwing the undefined TypeError
}
};
const func2 = (a2, d) => {
if (a2 && d) {
console.log("func2", d);
}
};
const func3 = (a2, d) => {
console.log("func3", d);
};
Logs
System Info
System:
OS: macOS 15.2
CPU: (12) arm64 Apple M2 Max
Memory: 79.14 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.11.0 - ~/.nvm/versions/node/v22.11.0/bin/node
npm: 10.9.0 - ~/.nvm/versions/node/v22.11.0/bin/npm
pnpm: 9.15.4 - ~/Library/pnpm/pnpm
bun: 1.1.34 - ~/.bun/bin/bun
Browsers:
Chrome: 132.0.6834.160
Safari: 18.2
npmPackages:
svelte: ^5.0.0 => 5.19.6Severity
annoyance
Metadata
Metadata
Assignees
Labels
No labels