Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
firefish5000 opened this issue Nov 7, 2020 · 2 comments · Fixed by #6529
Closed
Labels

Comments

@firefish5000
Copy link

firefish5000 commented Nov 7, 2020

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

@firefish5000 firefish5000 changed the title Destructuring directly into a store has undefined bahaviour Destructuring into a store without rename syntax results in undefined behavior Nov 7, 2020
@stale
Copy link

stale bot commented Jun 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@Conduitry
Copy link
Member

This should be fixed now in 3.40.3 - https://svelte.dev/repl/1e4f6ca9cf9c4f60ab48f40c5cf73d2a?version=3.40.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants