Closed
Description
Describe the bug
Destructuring into a svelte store directly, without typing the prop name in a rename syntax results in undefined behavior.
import {writable} from 'svelte/store'
let userName1 = writable('')
let userName2 = writable('')
let userName3 = writable('')
let obj = {userName1: 'user1', userName2: 'user2', userName3: 'user3'}
;({userName1: $userName1, $userName2 } = obj)
;({$userName3} = obj)
// $userName1 === 'user1'
// $userName2 === undefined
// $userName3 === obj
To Reproduce
https://svelte.dev/repl/1e4f6ca9cf9c4f60ab48f40c5cf73d2a?version=3.29.4
Expected behavior
$userName1
works as expected.
$userName2
and $userName3
are erroneous. I am not sure what behavior should be expected here, but what occurs is certainly not it. We could
- throw an error (possibly at compile time).
- Attempt to destructure the prop with the same name. That is,
$userName3
should look for a prop named$userName3
and get undefined (matching what appears to happen to userName2). This is a no magic solution following standard js behavior. - Attempt to access the prop with the same name as the store, without the dollar sign. So
$userName2
and$userName3
would getobj.userName2
andobj.userName3
respectively. This is a magic solution that makes sense combined with the inherit magic of stores.
Severity
Low. I think attempting to destructure in any way other than the way I did with $userName1
is asking for trouble. I have no intention of trying to de-structure into stores any other way, if I destructure into stores at all.
Additional context
Found due to question asked by OwnageJuice