Skip to content

Destructuring into a store without rename syntax results in undefined behavior #5653

Closed
@firefish5000

Description

@firefish5000

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

  1. throw an error (possibly at compile time).
  2. 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.
  3. Attempt to access the prop with the same name as the store, without the dollar sign. So $userName2 and $userName3 would get obj.userName2 and obj.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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions